Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math operator in specifying figure width in LaTeX

Tags:

latex

In LaTeX figures, one can use \textwidth and \columnwidth to specify sizes of the graphic relative to the size of surrounding text, e.g. \includegraphics[width=.5\textwidth]{myimage}.

I would like to easily switch from onecolumn to twocolumn template (and back) without the figure growing too large for onecolumn template. For twocolumn template (where \columnwidth is roughly half the \textwidth), I would like to have something like: \includegraphics[width=.9\columnwidth]{myimage}. and for onecolumn template (where \columnwidth and \textwidth are equal):
\includegraphics[width=.5\textwidth]{myimage}.

Now, I figured I could limit this using some kind of a min operator: \includegraphics[width=min(.5\textwidth,.9\columnwidth)]{myimage} but this is invalid syntax. Is there something like this to solve this problem, possibly through the use of LaTeX macro system?

like image 952
mbudisic Avatar asked Mar 07 '09 21:03

mbudisic


People also ask

How do I resize a figure in LaTeX?

Use the scale=1.5 option in the \includegraphics command to resize the image to 150% of its original size. That is, \includegraphics[width=50mm,scale=1.5]{method. eps}. You can use a different percentage if needed.

How do I change the width and length of an image in LaTeX?

The command \includegraphics[scale=1.5]{overleaf-logo} will include the image overleaf-logo in the document, the extra parameter scale=1.5 will do exactly that, scale the image 1.5 of its real size. You can also scale the image to a some specific width and height.

How do I center a figure in LaTeX?

Centering. We center text or images using \begin{center} and \end{center}. Just put \begin{center} when you want to start centering, and \end{center} when you want to stop centering.


3 Answers

\textwidth is the horizontal width of the page body and not really appropriate for your purposes.

\linewidth is the width of the current line; it will be updated appropriate to columns, indentation, etc.

The following paragraph produces a picture that should precisely fit the entire line width (i.e. no overful warning):

\noindent\includegraphics[width=\linewidth]{myimage}

If you prefer small margins on the left and right, you can use:

\begin{center}
\includegraphics[width=.9\linewidth]{myimage}
\end{center}

Or, if you want to specify the margins in an absolute size:

\usepackage{calc}
...
\begin{center}
\includegraphics[width=\linewidth-20pt]{myimage}
\end{center}
like image 31
Bruno De Fraine Avatar answered Sep 23 '22 11:09

Bruno De Fraine


Although it's possible to write this sort of macro, I wouldn't want to hardcode it into each figure; how about something like this

\makeatletter
\newlength \figwidth
\if@twocolumn
  \setlength \figwidth {0.9\columnwidth}
\else
  \setlength \figwidth {0.5\textwidth}
\fi
\makeatother

and then use

\includegraphics[width=\figwidth]{myimage}

to insert the graphic.

like image 75
Will Robertson Avatar answered Sep 26 '22 11:09

Will Robertson


Hmm... the code above (\if@twocolumn etc.) is not working for me at all. No idea why not. :( tetex on osX using fink. Trying to use revtex4, so perhaps that's the problem. I really like the idea of this type of change because I'm going to be dorking with widths etc. for my thesis and various journal articles, and to have these distances specified with a macro may be helpful for these types of conversions.

Any comments greatly appreciated! -Allen

like image 20
AllenH Avatar answered Sep 25 '22 11:09

AllenH