6

I am looking to create a command that adjusts my page layout, here is a sample:

\newcommand{\margin}{
\pdfpageheight 297mm
\pdfpagewidth 210mm
\pagebreak
\newgeometry{lmargin=31.5mm, rmargin=31.5mm, includemp, marginparsep=5mm, marginparwidth=52mm}
\setlength{\headwidth}{147mm}}

It basically just adjusts the margins to have a 52mm margin on the right hand side, and then I have other layouts too, which I all call with different commands, such as \margin, \nomargin, \marginlandscape, and this brings me to my problem.

Ideally, I would want to only have to use one command, such as

\mypagestyle{margin}
\mypagestyle{nomarginlandscape}

but I have no idea how to create such a command, I've tried to do it myself and I've looked all over the place, but can find no answers. So, how do I code a newcommand that would give me the desired results?

2 Answers 2

6

You can use the same mechanism as used for \pagestyle (using its original simpler definition without error checking here)

\def\mypagestyle#1{\csname myps#1\endcsname}

then just define \mypsmargin \mypsnomarginlandscape to do whatever ever you want and then you can use the syntax

\mypagestyle{nomarginlandscape}

to call the internal command \mypsnomarginlandscape

2
  • I'm not sure I quite understand, sorry, I'm new to Tex. Where would I define nomargin, etc? And then how would I add a new definition, of say nomarginlandscape?
    – akuritsu
    Commented Jul 10, 2013 at 6:31
  • Just change the definition in your question from \newcommand{\margin}{ to \newcommand{\mypsmargin}{ then \mypagestyle{margin} will work. Add similar definitions at the same place Commented Jul 10, 2013 at 13:27
2

This might be overkill, but I normally use the xstring package for these sort of thing:

Further Enhancements:

  • This only tests for margin but this could easily be extended to support multiple values. If there is just two cases then simply add and another \IfStrEq, but for more than two options I would recommend using the IfStrEqCase See for instance Checking if a macro expands to one of two given strings.

Code:

\documentclass{article}
\usepackage{geometry}
\usepackage{xstring}

\newcommand{\mypagestyle}[1]{%
    \IfStrEq{#1}{margin}{%
       Setting "#1": % Adjust settings here
       \newgeometry{lmargin=31.5mm, rmargin=31.5mm, includemp, marginparsep=5mm, marginparwidth=52mm}
    }{%
       Setting "#1": % Adjust settings here
       \newgeometry{lmargin=31.5mm, rmargin=31.5mm, includemp, marginparsep=5mm, marginparwidth=52mm}    }%
}%

\begin{document}
\mypagestyle{margin}

\mypagestyle{nomarginlandscape}
\end{document}
1
  • That looks good, but do you know of a way where I do not need any extra packages? Thanks for the prompt response though.
    – akuritsu
    Commented Jul 10, 2013 at 0:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .