tags:

views:

4310

answers:

4

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?

A: 

Somebody else who is more familiar with this will probably answer, but note that you would also need to change your figure type to be figure* if you are going two-column.

Uri
I am not doing this without seeing any adverse effects. Can you clarify why one would do this?
mbudisic
OK, I've read http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Wide_figures_in_two_column_documentsto see what you mean. Note that I want my figures to fit within a single column in two-column mode, and stretch in onecolumn mode, but not so much that they take the whole page width.
mbudisic
+6  A: 

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.

Will Robertson
Should be \if@twocolumn, but otherwise a good answer.
ChrisN
Ha, excellent! This looks much nicer than some weird answers I've found online. Thank you very much!
mbudisic
Now I've tested it and it worked with two alterations: first, the already mentioned `@twocolumn`. Second, `\linewidth` needed to be changed to `\textwidth`, otherwise the image was shown only in twocolumn mode. For completeness, I am using IEEEtran template, used by IEEE journals an confs. Thanks!
mbudisic
@mbudisic If this answer has solved your problem, please mark it as "accepted".
ChrisN
Yes, I did, thanks. I'm still new to this website.
mbudisic
Fixed those dumb typos, by the way. Thanks for the catch.
Will Robertson
A: 

\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}
Bruno De Fraine
A: 

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

AllenH
What error are you getting? Are you protecting this code with `\makeatletter`?
Charles Stewart
Yup, added it as above, and used it as per example- no love. I think it's probably revtex4 playing with me. I'll have to try an example article class I should add no error was thrown- it just ignored the command completely. I'll try again soon and post back. Thank you for asking!
AllenH