Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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

share|improve this answer
    
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 Jul 10 '13 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 –  David Carlisle Jul 10 '13 at 13:27

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}
share|improve this answer
    
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 Jul 10 '13 at 0:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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