# Is it possible to make \usepackage depend on paper size? [duplicate]

I currently have a document (.tex source files), where I define the paper size and one sided design as class options. I use the KOMA-Script class scrbook.

• in A4 format for printing on usual printers at university and watching at the screen

• in A5 format for professional printing (well, it was not printed so far, but I would like to try this)

At the moment, I have a TODO list what to change when I switch from A4 toA5:

* In GeoTopo.tex...
* ... replace a4paper by a5paper,
* ... replace oneside by twoside and
* ... replace \usepackage[...]{hyperref} by \usepackage{nohyperref}
* In titlepage.tex: replace 10cm by 4cm


Is there any (La)TeX solution so that I would only have one thing to get these different configurations?

I think it might look like this:

\newcommand{\CompileConfiguration}{A4}
...
\if\CompileConfiguration==A4
\usepackage[...]{hyperref}
\else
\usepackage{nohyperref}
\fi


My thirst thought was to make it dependent on the paper size. But I have no idea how the condition would look like in this case.

An even better solution would define a variable even before \documentclass. But I have never seen anything before \documentclass except nag, so I doubt this is possible. Especially, even for the example above, I don't know the details. Is it == for comparison or =?

I have seen

\ifundefined
\ifnum
\ifodd
\ifdim
\iftrue
\iffalse
\ifx


so maybe there is no string comparison. But I don't know what other type \CompileConfiguration has (or how I could define it e.g. as a boolean)

## marked as duplicate by Martin Thoma, Jesse, Claudio Fiandrino, Svend Tveskæg, ThorstenJan 24 '14 at 14:37

• I would think some sort of boolean (\if<something> ... \else ... \fi) would be the easiest way. etoolbox also provides lots of advanced options. – jon Jan 23 '14 at 21:34
• You might want to have a look at this – Alex R. Jan 23 '14 at 21:35
• You can make a custom class defining the option(s) you want and then pass the relevant options onto another class and packages. That is pretty easy to set up. Although it is a custom class, you don't actually do more than just load the class(es) and package(s) of your choice with the options you've selected. – cfr Jan 23 '14 at 21:42
• You can; try \newif\ifxxx\xxxtrue\ifxxx\def\sth{something}\else\fi before \documentclass: only if \xxxtrue is not included, will you get an error. (Linebreaks are not provided because of the way comments are formatted.) – jon Jan 23 '14 at 21:42
• @Sepravir: No, I do not include \usepackage{geometry}. I've added a link to the document so you can take a look what I currently have in my preamble. It's pretty big and I thought this is not directly related to my question, so I did not include the document itself. – Martin Thoma Jan 23 '14 at 22:10

As already Jon and Alex R. explicitely or implicitely wrote in their comments, this can very well done with conditionals. As I discovered you already load the packages etoolbox and xifthen (which loads in background ifthen). One could also use the pure TeX version. etoolbox provides two different kinds of conditionals, beside TeX Booleans also LaTeX toggles (more see in documentation under “Boolean Flags”). I will try to show here all three/four. Which one you use, is mostly a matter of taste:

First define a Boolean. This must be done very early, at least before the first use of \KOMAoptions, see below. Of course in the case of etoolbox and xifthen the package must be loaded before. Note that for the TeX flags both packages define the pure TeX Boolean in the background, so putting all the following together into one file will produce errors.

% pure TeX
\newif\ifAFive

% (x)ifthen
\newboolean{AFive}

% etoolbox TeX flag
\newbool{AFive}

% etoolbox LaTeX flag
\newtoggle{AFive}


Now change your first line to

\documentclass[DIV15,BCOR12mm]{scrbook}


and somewhere later in the preamble (again: for etoolbox and xifthen after package loading) you add this:

% pure TeX
\ifAFive
\KOMAoptions{paper=a5,twoside=true}
\else
\KOMAoptions{paper=a4,twoside=false}
\fi

% (x)ifthen
\ifthenelse{\boolean{AFive}}{
\KOMAoptions{paper=a5,twoside=true}
}{
\KOMAoptions{paper=a4,twoside=false}
}

% etoolbox TeX flag
\ifbool{AFive}{
\KOMAoptions{paper=a5,twoside=true}
}{
\KOMAoptions{paper=a4,twoside=false}
}

% etoolbox LaTeX flag
\iftoggle{AFive}{
\KOMAoptions{paper=a5,twoside=true}
}{
\KOMAoptions{paper=a4,twoside=false}
}


You can easily add hyperref options here. This package provides a command \hypersetup to change or add options after package loading. Because I suspect you only want to deactivate coloured links for print I suggest you using the option hidelinks. Add this after loading of hyperref, where you probably have a option colorlinks (I only show two versions, the others should be clear from above):

% pure TeX
\ifAFive
% no \else branch needed in this case
\fi

% (x)ifthen
\ifthenelse{\boolean{AFive}}{
}{} % the part for Boolean=false is left empty


If you really want to load nohyperref, then simply replace the \hypersetup line. You can merge this with the conditional calls from above, as long as they are placed after all needed packages.

Later on command line for compiling in A4 format you need to change nothing (Booleans and toggles are false, if not explicitely set to true). For A5 format you now compile (with double quotes in Windows, replace them with single quotes in Linux and MacOSX):

• when you use the pure flag:

pdflatex "\AFivetrue\input{GeoTopo.tex}"

• when you use the (x)ifthen version:

pdflatex "\setboolean{AFive}{true}\input{GeoTopo.tex}"

• when you use the etoolbox TeX flag:

pdflatex "\setbool{AFive}{true}\input{GeoTopo.tex}"

• when you use the etoolbox LaTeX flag:

pdflatex "\settoggle{AFive}{true}\input{GeoTopo.tex}".

For etoolbox there are also the variants \booltrue{AFive} respective \toggletrue{AFive}.

Edit: I forgot titlepage.tex.

The commands of this short file I would merge into the main file. The \vspace inside of the title definition is wrong I’m sure. You would need an empty line or a \par after it, but (without newly testing) I guess you would get an error in the title. Use the optional argument of the newline command \\[10cm] or, because we want to change this:

Define somewhere in the preamble a new length

\newlength{\timgskip} % from "title image skip"


Then again in conditionals (this time only pure version shown):

\ifAFive
\setlength{\timgskip}{4cm}
\else
\setlength{\timgskip}{10cm}
\fi


This can also merged, as long as it is set after the new length definition.

The according line now must now look like this:

\title{Geometrie und Topologie\\[\timgskip]

• BTW I personally would favour etoolbox. – Speravir Jan 24 '14 at 1:23
• Thanks for your efforts (+1), but I've just found tex.stackexchange.com/q/5894/5645 while trying to figure out how \newif can be defined to false. So I think this question should get closed. – Martin Thoma Jan 24 '14 at 14:05
• Thanks, that you’ve accepted nonetheless. And I hope my other advices are also helpful. – Speravir Jan 24 '14 at 17:19

You could do something like this (note, however, that I don't use the KOMA classes, so I'm not familiar with all the page-related options):

\newif\ifaiv
%\aivtrue    % default is \aivfalse
% pre-\documentclass usage
\ifaiv
\def\pagedim{a4paper}
\else
\def\pagedim{paper=A5}
\fi

\documentclass[\pagedim]{scrbook}
\usepackage[T1]{fontenc}

% in-preamble usage
\ifaiv