7

I've never done any coding in Latex than involves anything too complex (anything more than just writing and customizing a few stuff here and there), so I'm having troubles finding what I need to do this.

I need my project to compile in two size, a4paper and a5paper, passed as an option to the \documentclass[]{article} command.

I just select and option, produce a pdf, then change it to a5 and compile again. Now there are some parts of the document that should be different if it's on a5 or a4 mode, and I don't know how to do it.

How could I do so? Something like

\if @pagesize=a4paper
    do stuff
\fi

that works?

Thank you in advance.

EDIT: I've read that the old PlainTeX conditionals can lead to unexpected behaviour in some cases, but what I would do inside would be really simple, like putting \hspaces and things like that, so if it's simple with those conditional, I'd like to use them. Obviously if it can be better done any other way, I would like to know it too.

5

As far as I know, the paper size options are not directly stored in a macro, but this could work.

I've introduced a plain \newif\ifafourpaper which defaults to false and is set to true, if the a4paper option is used.

From article.cls, I stole the \DeclareOption for a4paper and a5paper respectively, where the boolean macro \ifafourpaper is set accordingly.

Important

It's necessary to use \ProcessOptions explicitly, since the options declarations has changed!


\documentclass[a5paper]{article}

\usepackage{blindtext}

\newif\ifafourpaper

\afourpaperfalse


\DeclareOption{a4paper}
   {\setlength\paperheight {297mm}%
    \setlength\paperwidth  {210mm}%
    \afourpapertrue%
  }
\DeclareOption{a5paper}
   {\setlength\paperheight {210mm}%
    \setlength\paperwidth  {148mm}%
    \afourpaperfalse%
  }


\ProcessOptions

\begin{document}
\ifafourpaper
\blindtext[2]
\else
\blindtext
\fi

\end{document}
  • @MyUserIsThis: You're welcome! Happy TeXing. – user31729 May 31 '15 at 22:12
  • I'm sorry, but it always considers \afourpapertrue, also in a5paper mode. I've included de \ProcessOptions line – MyUserIsThis May 31 '15 at 22:13
  • @MyUserIsThis: Yes, there was a small typo in my code. I updated already some 5 minutes ago – user31729 May 31 '15 at 22:14
  • @MyUserIsThis: No problem, my typo caused your distraction ;-) – user31729 Jun 1 '15 at 1:10
  • 1
    I suggest using \DeclareOption{a4paper}{\afourpapertrue\DeclareOption{a4paper}\PassOptionsToClass{a4paper}{article}} and \DeclareOption{a5paper}{\afourpaperfalse\DeclareOption{a5paper}\PassOptionsToClass{a5paper}{article}} as there may be other things that the document class does with the a4/a5 options. In fact, this doesn't seem to be the case with the article class but it might be for other document classes. – Andrew Jun 1 '15 at 10:10
4

Assuming you aren't using landscape mode, you can base your decision on the paper height as stored by the class.

\documentclass[a4paper]{article}

\newcommand\papersize[1]{%
  TT\fi
  \uppercase{\ifdim\csname #1@PAPER\endcsname}=\paperheight
}
\makeatletter
\@namedef{A4@PAPER}{297mm}
\@namedef{A5@PAPER}{210mm}
\@namedef{B5@PAPER}{250mm}
\@namedef{LETTER@PAPER}{11in}
\@namedef{LEGAL@PAPER}{14in}
\@namedef{EXECUTIVE@PAPER}{10.5in}
\makeatother


\begin{document}

\if\papersize{a4}
  We're using A4 paper
\else
  We're not using A4 paper
\fi

\if\papersize{A5}
  We're using A5 paper
\else
  We're not using A5 paper
\fi

\end{document}

enter image description here

The output changes to

enter image description here

if \documentclass[a5paper]{article} is used.

Note that you're free to use either A5 or a5; I defined values for the arguments corresponding to a4, a5, b5, letter, legal and executive, which are all the sizes known to article and the other standard classes. Other values can be added at will.

  • Thank you for your answer. I will surely use it in the future, as it extends a little bit the first answer. Is there any reason you define all of them in capital letters? Does LaTeX differentiate between capital and small letters? – MyUserIsThis Jun 1 '15 at 13:56
  • @MyUserIsThis I decided to support both uppercase and lowercase letters because the “official” names for ISO paper formats are A0, A1, A2, A3, A4 and so on. With that definition, which however has the defect of not being fully expandable, you need not worry about the capitalization. If you need a fully expandable version, the \uppercase trick must be removed. – egreg Jun 1 '15 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.