# [Tugindia] Using conditionals

Nina Mazumdar nina.mazumdar at gmail.com
Mon Sep 15 04:53:06 CEST 2008

On Sun, Sep 14, 2008 at 8:39 PM, Kannan Moudgalya <kannan at iitb.ac.in> wrote:
> Nina Mazumdar wrote:
>> On Sat, Sep 13, 2008 at 11:12 PM, Kannan Moudgalya <kannan at iitb.ac.in> wrote:
>>
>>> I want to know how to use the conditionals in LaTeX.  For example, if I
>>> have the \multicols option on, I want the width of the inserted figures
>>> to be \linewidth.  If this option is off, the width should become
>>> 0.5\linewidth.
>>>
>> Do we need to go for any conditional command? What we need is nothing
>> but a new dimension defined which is exactly the column width.
>>
>> Here is one way of doing it:
>>
> Dear Nina,
>
> Thanks for your mail.  The above solution will work if the same length
> is to be used in one column and two column formats.  I often have to use
> different sizes, however.  For example, while in two column mode I may
> use 0.75\Columnwidth, I would like to use something larger for one
> column mode, because, the above size is rather small in one column mode.
>  Of course, 0.75\linewidth will become rather large in one column format.

Here is another way of doing it:

\makeatletter
%
% multicols tweaks
%
\let\savemulticols\multicols
\let\saveendmulticols\endmulticols
\newif\if at multicols \@multicolsfalse
\def\multicols{\global\@multicolstrue\savemulticols}
\def\endmulticols{\saveendmulticols\global\@multicolsfalse}

%
% two scratch dimen registers
%
\newdimen\scolwidth \scolwidth=\linewidth % width for single column figure
\newdimen\dcolwidth \dcolwidth=.5\linewidth % width for double column figure

%
% graphics inclusion macro which takes the dimensions
%
\def\Includegraphics{\@ifnextchar[{\@Includegraphics}{\@@Includegraphics}}

% Usage of \@Includegraphics is
% \Includegraphics[doublecolumnwidth,singlecolumnwidth]{figurefile}
% \Inlcudegraphics[.8\linewidth,.3\linewidth]{filename}
%
\def\@Includegraphics[#1,#2]#3{%
\@ifundefined{if at multicols}
{\if at twocolumn
\includegraphics[width=#1]{#3}
\else
\includegraphics[width=#2]{#3}
\fi}
{\if at multicols
\includegraphics[width=#1]{#3}
\else
\includegraphics[width=#2]{#3}
\fi}
}

%
% This takes the figure width dimension from \scolwidth and \dcolwidth
% which you can define at the beginning and change anywhere in the document.
% The usage of \@@Includegraphics is:
% \dcolwidth=2in  % or whatever
% \scolwidth=3in  % or whatever
% The above is optional, if you don't provide the previous length will be taken
% \Includegraphics{filename}
%
\def\@@Includegraphics#1{%
\@ifundefined{if at multicols}
{\if at twocolumn
\includegraphics[width=\dcolwidth]{#1}
\else
\includegraphics[width=\scolwidth]{#1}
\fi}
{\if at multicols
\includegraphics[width=\dcolwidth]{#1}
\else
\includegraphics[width=\scolwidth]{#1}
\fi}
}

\makeatother

The above macros will work in both \multicols or \twocolumn modes
without any tweaking.
Please get back, if you need further facilities.

Happy TeXing and best regards

--
Nina