1

I would like to change some document margins with only one switch. However, the macro does not seem to be evaluated?! Below, I only show 1 of 3 values that should be controlled by one variable.

\newcommand{\headsepValue}[3]{%
\IfEqCase{#1}{%
    {a}{43mm}% \headsep{43mm}
    {b}{53mm}% \headsep{53mm}
    {c}{63mm}% \headsep{63mm}
    % you can add more cases here as desired
}[\PackageError{headsepValue}{Undefined option to headsepValue: #1}{}]%
}%

\newcommand{\valueUsedForSpacing}{a}

\setlength{\headsep}{\headsepValue{\valueUsedForSpacing}}

This gives me an error (! Missing number, treated as zero). Do I have to use \expandafter somehow? Or how do I tell Latex that I want:

\setlength{\headsep}{\headsepValue{\valueUsedForSpacing}}

which should evaluate to

\setlength{\headsep}{\headsepValue{a}}

and then

\setlength{\headsep}{43mm}

Thanks for your help!

Update: Thanks for all the replies so far! I will take a closer look at the proposed alternatives later. But first I provide you with my code.

I make use of g-brief and try to modify it a bit. I added example.tex and the g-brief.cls, which gives me the error.

This is example.tex:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                       PREAMBLE                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[english,10pt]{g-brief}
\renewcommand{\familydefault}{\sfdefault}
\newcommand*{\myfont}{\fontfamily{ptm}\selectfont}      %font in header and footer ptm=Times
\usepackage{helvet}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage{pgfplots}

\pgfplotsset{compat=1.7}
\usetikzlibrary{fadings}

\lochermarke
\faltmarken
%\fenstermarken
\trennlinien
%\klassisch

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%               Body                                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\Name{MyName}
\Strasse{MyAddress}
%\Zusatz{}
\Ort{MyPlace}
\Land{MyCountry}
\Telefon{MyPhoneNumber}
%\Telex{}
\EMail{MyEmail}
\HTTP{MyHTTP}
%\Bank{}
%\BLZ{}
%\Konto{}
\RetourAdresse{ } % Wichtig: Muss ein Leerzeichen sein, sonst gibt's
                  % Probleme mit dem Brief-Fenster, falls ein Land
                  % angegeben wird

\Adresse{YourName\\
YourAddress\\
YourCountry\\
}

\Datum{\today}
%\Postvermerk{}
%\MeinZeichen{}
\Betreff{Modify g-brief.cls}
%\IhrSchreiben{}
%\Anlagen{CV attached}
%\Verteiler{}

\Anrede{GreetingToYou,}
\Gruss{ByeBye}{0.2cm}
\Unterschrift{MySignature}

\begin{g-brief}

If I have lot of text here, I want to be able to adjust several margins to squeeze everything on one single page instead of ending up with e.g. a few words on the next page

Margins that I would like to change are: 1) separation between header and    address field, 2) based on that change the available text height [and 3) should actually be the vertical position of the line separating the footer, which is linked to the text height]

\end{g-brief}
\end{document}

... and this is my modified g-brief.cls

%%
%% This is file `g-brief.cls',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% g-brief.dtx  (with options: `class')
%% 
%% =======================================================================
%% 
%% Copyright (C) 1991-2003 Michael Lenzen.
%% 
%% For additional copyright information see further down in this file.
%% 
%% This file is part of the G-BRIEF package.
%% -----------------------------------------------------------------------
%% 
%% It may be distributed under the terms of the LaTeX Project Public
%% License (LPPL), as described in lppl.txt in the base LaTeX distribution.
%% Either version 1.1 or, at your option, any later version.
%% 
%% The latest version of this license is in
%% 
%%          http://www.latex-project.org/lppl.txt
%% 
%% LPPL Version 1.1 or later is part of all distributions of LaTeX
%% version 1999/06/01 or later.
%% 
%% 
%% Error reports in case of UNCHANGED versions to
%% 
%%                            <lenzen@lenzen.com>
%%                            <m.lenzen@t-online.de>
%% 
%% 
\usepackage{xstring}
\def\filedate{2008/07/15}
\def\fileversion{4.0.2}
\def\filedate{2008/07/15}
\def\fileversion{4.0.2}
\def\filename{g-brief.cls}
\NeedsTeXFormat{LaTeX2e}[2001/06/01]

\ProvidesClass{g-brief}[\filedate\space v\fileversion\space g-brief class]

\newdimen\VorschubH
\newdimen\VorschubV
\VorschubH = 0.0mm
\VorschubV = 0.0mm

\def\sprache{ngerman}
\def\telefontex{{\footnotesize Telefon\/:}}
\def\telefaxtext{{\footnotesize Telefax\/:}}
\def\telextext{{\footnotesize Telex\/:}}
\def\emailtext{{\footnotesize e--Mail\/:}}
\def\httptext{{\footnotesize HTTP\/:}}
\def\banktext{{\footnotesize Bankverbindung\/:}}
\def\blztext{{\footnotesize BLZ\/:}}
\def\kontotext{{\footnotesize Kto.\/:}}
\def\betrefftext{{\normalsize Betreff\/:}}
\def\ihrzeichentext{\footnotesize\textsc{Ihr Zeichen}}
\def\ihrschreibentext{\footnotesize\textsc{Ihr Schreiben vom}}
\def\meinzeichentext{\footnotesize\textsc{Mein Zeichen}}
\def\unserzeichentext{\footnotesize\textsc{Unser Zeichen}}
\def\datumtext{\footnotesize\textsc{Datum}}
\def\datum{\number\day.~\ifcase\month\or Januar\or Februar\or
    M\"arz\or April\or Mai\or Juni\or Juli\or August\or September\or
    Oktober\or November\or Dezember\fi \space\number\year}

\DeclareOption{english}{
    \def\sprache{english}
    \def\telefontex{{\footnotesize Phone\/:}}
    \def\telefaxtext{{\footnotesize Fax\/:}}
    \def\telextext{{\footnotesize Telex\/:}}
    \def\emailtext{{\footnotesize e--Mail\/:}}
    \def\httptext{{\footnotesize HTTP\/:}}
    \def\banktext{{\footnotesize Bank\/:}} \def\blztext{{\footnotesize
            code}} \def\kontotext{{\footnotesize acct}}
    \def\betrefftext{{\normalsize Subject:}}
    \def\ihrzeichentext{\footnotesize\textsc{Your Ref.}}
    \def\ihrschreibentext{\footnotesize\textsc{Your Mail}}
    \def\meinzeichentext{\footnotesize\textsc{My Ref.}}
    \def\unserzeichentext{\footnotesize\textsc{Our Ref.}}
    \def\datumtext{\footnotesize\textsc{Date}}
    }

\DeclareOption{american}{
    \def\sprache{american}
    \def\telefontex{{\footnotesize Phone\/:}}
    \def\telefaxtext{{\footnotesize Fax\/:}}
    \def\telextext{{\footnotesize Telex\/:}}
    \def\emailtext{{\footnotesize e--Mail\/:}}
    \def\httptext{{\footnotesize HTTP\/:}}
    \def\banktext{{\footnotesize Bank\/:}} \def\blztext{{\footnotesize
            code}} \def\kontotext{{\footnotesize acct}}
    \def\betrefftext{{\normalsize Subject:}}
    \def\ihrzeichentext{\footnotesize\textsc{Your Ref.}}
    \def\ihrschreibentext{\footnotesize\textsc{Your Mail}}
    \def\meinzeichentext{\footnotesize\textsc{My Ref.}}
    \def\unserzeichentext{\footnotesize\textsc{Our Ref.}}
    \def\datumtext{\footnotesize\textsc{Date}}
    }

\DeclareOption{10pt}{\def\@schrift{0}}
\DeclareOption{11pt}{\def\@schrift{1}}
\DeclareOption{12pt}{\def\@schrift{2}}

\let\@docencoding\@empty
\DeclareOption{ansinew}{\def\@docencoding{ansinew}}
\DeclareOption{applemac}{\def\@docencoding{applemac}}
\DeclareOption{ascii}{\def\@docencoding{ascii}}
\DeclareOption{cp1250}{\def\@docencoding{cp1250}}
\DeclareOption{cp1252}{\def\@docencoding{cp1252}}
\DeclareOption{cp437de}{\def\@docencoding{cp437de}}
\DeclareOption{cp437}{\def\@docencoding{cp437}}
\DeclareOption{cp850}{\def\@docencoding{cp850}}
\DeclareOption{cp852}{\def\@docencoding{cp852}}
\DeclareOption{cp865}{\def\@docencoding{cp865}}
\DeclareOption{decmulti}{\def\@docencoding{decmulti}}
\DeclareOption{latin1}{\def\@docencoding{latin1}}
\DeclareOption{latin2}{\def\@docencoding{latin2}}
\DeclareOption{latin3}{\def\@docencoding{latin3}}
\DeclareOption{latin4}{\def\@docencoding{latin4}}
\DeclareOption{latin5}{\def\@docencoding{latin5}}
\DeclareOption{latin9}{\def\@docencoding{latin9}}
\DeclareOption{next}{\def\@docencoding{next}}

\ExecuteOptions{11pt,@sprache} \ProcessOptions
\LoadClass[1\@schrift pt, a4paper, oneside, final]{letter}[1999/04/29]
\normalsize

\newcommand{\headsepValue}[3]{%
        \IfEqCase{#1}{%
                {a}{43mm}% \headsep{43mm}
                {b}{53mm}% \headsep{53mm}
                {c}{63mm}% \headsep{63mm}
                % you can add more cases here as desired
        }[\PackageError{headsepValue}{Undefined option to headsepValue: #1}{}]%
}%

\newcommand{\txtHeight}[3]{%
        \IfStrEqCase{#1}{%
                {a}{208.88mm}% \headsep{43mm}
                {b}{198.88mm}% \headsep{53mm}
                {c}{188.88mm}% \headsep{63mm}
                % you can add more cases here as desired
        }[\PackageError{txtHeight}{Undefined option to txtHeight: #1}{}]%
}%
\newcommand{\yPosition}[3]{%
        \IfStrEqCase{#1}{%
                {a}{-770pt}% \headsep{43mm}
                {b}{-760pt}% \headsep{53mm}
                {c}{-750pt}% \headsep{63mm}
                % you can add more cases here as desired
        }[\PackageError{yPosition}{Undefined option to yPosition: #1}{}]%
}%

\newcommand{\valueUsedForSpacing}{a}

\setlength\oddsidemargin{0\p@}
\setlength\evensidemargin{\oddsidemargin}
\setlength\marginparwidth{90\p@}
\setlength\marginparsep{11\p@}
\setlength\marginparpush{5\p@}
\setlength\topmargin{-12.69mm}
\setlength\headheight{4.23mm}

%\setlength{\headsep}{43mm} % 43mm=a,(53mm=b),63mm=c; 
\setlength{\headsep}{\headsepValue{\valueUsedForSpacing}}

\setlength\topskip{3.38mm}
\addtolength\footskip{4.23mm}
\setlength\textheight{208.88mm} %43mm=208.88mm,(53mm=198.88mm),63mm = 188.88mm
\setlength\textwidth{165mm}

\setlength\lineskip{1\p@}
\setlength\normallineskip{1\p@}

\renewcommand\baselinestretch{1}

\advance\topmargin by \VorschubV
\advance\oddsidemargin by \VorschubH
\setlength\evensidemargin{\oddsidemargin}

\AtEndOfClass{\pagestyle{regularpage}}

\def\Name#1{\def\name{#1}} \def\name{}
\def\Strasse#1{\def\strasse{#1}} \def\strasse{}
\def\Zusatz#1{\def\zusatz{#1}} \def\zusatz{}
\def\Ort#1{\def\ort{#1}} \def\ort{}
\def\Land#1{\def\land{#1}} \def\land{}
\def\RetourAdresse#1{\def\retouradresse{#1}} \def\retouradresse{}

\def\Telefax#1{\def\telefax{#1}} \def\telefax{}
\def\Telex#1{\def\telex{#1}} \def\telex{}
\def\EMail#1{\def\email{#1}} \def\email{}
\def\HTTP#1{\def\http{#1}} \def\http{}

\def\Bank#1{\def\bank{#1}} \def\bank{}
\def\BLZ#1{\def\blz{#1}} \def\blz{}
\def\Konto#1{\def\konto{#1}} \def\konto{}

\def\Postvermerk#1{\def\postvermerk{#1}} \def\postvermerk{}
\def\Adresse#1{\def\adresse{#1}} \def\adresse{}

\def\IhrZeichen#1{\def\ihrzeichen{#1}} \def\ihrzeichen{}
\def\IhrSchreiben#1{\def\ihrschreiben{#1}} \def\ihrschreiben{}
\def\MeinZeichen#1{\def\meinzeichen{#1}} \def\meinzeichen{}
\def\Datum#1{\def\datum{#1}} \def\datum{\today}

\def\Betreff#1{\def\betreff{#1}} \def\betreff{}

\def\Anrede#1{\def\anrede{#1}} \def\anrede{}
\def\Gruss#1#2{\def\gruss{#1} \def\grussskip{#2}}
        \def\gruss{} 
        \def\grussskip{}    

\def\Unterschrift#1{\def\unterschrift{#1}} \def\unterschrift{}

\def\Anlagen#1{\def\anlagen{#1}} \def\anlagen{}
\def\Verteiler#1{\def\verteiler{#1}} \def\verteiler{}

\long\def\Einrueckung#1{\par\begin{tabular}{@{\hspace{1in}}p{5in}@{}}
        #1\end{tabular}\par}

\newif\ifklassisch\klassischfalse
\def\klassisch{\klassischtrue}

\newif\iftrennlinien\trennlinienfalse
\def\trennlinien{\trennlinientrue}

\newif\iflochermarke\lochermarkefalse
\def\lochermarke{\lochermarketrue}

\newif\iffaltmarken\faltmarkenfalse
\def\faltmarken{\faltmarkentrue}

\newif\iffenstermarken\fenstermarkenfalse
\def\fenstermarken{\fenstermarkentrue}

\newif\ifunserzeichen\unserzeichenfalse
\def\unserzeichen{\unserzeichentrue}

\newenvironment{g-brief}{
    \thispagestyle{firstpage}
    \setlength\unitlength{1mm}

    \c@page\@ne
    \interlinepenalty=200
    \clearpage

    \ifx \ihrzeichen\empty \ifx \ihrschreiben\empty \ifx
    \meinzeichen\empty \makebox[50.5mm][l]{} \makebox[55.0mm][l]{}
    \makebox[20.0mm][l]{} \else \makebox[45.5mm][l]{\ihrzeichentext}
    \makebox[55.0mm][l]{\ihrschreibentext} \ifunserzeichen
    \makebox[20.0mm][l]{\unserzeichentext} \else
    \makebox[20.0mm][l]{\meinzeichentext} \fi \fi \else
    \makebox[45.5mm][l]{\ihrzeichentext}
    \makebox[55.0mm][l]{\ihrschreibentext} \ifunserzeichen
    \makebox[20.0mm][l]{\unserzeichentext} \else
    \makebox[20.0mm][l]{\meinzeichentext} \fi \fi \else
    \makebox[45.5mm][l]{\ihrzeichentext}
    \makebox[55.0mm][l]{\ihrschreibentext} \ifunserzeichen
    \makebox[20.0mm][l]{\unserzeichentext} \else
    \makebox[20.0mm][l]{\meinzeichentext} \fi \fi
    \makebox[37.0mm][r]{\datumtext} \\
    \makebox[45.5mm][l]{\ihrzeichen}
    \makebox[55.0mm][l]{\ihrschreiben}
    \makebox[20.0mm][l]{\meinzeichen}
    \makebox[42.0mm][r]{\datum} \par

    \vspace{8mm}

    \ifcase\@ptsize\vspace{0.045pt}\or \vspace{-1.555pt}\or
    \vspace{-2.955pt} \fi

    \ifx \betreff\empty 
            \else \ifklassisch 
                    \betrefftext \space \space \betreff 
                \else 
                    \textbf{\betreff} 
                \fi 
        \mbox{} \par \vspace{-1\parskip} \vspace{8mm} 
    \fi

    \ifcase\@ptsize\vspace{0.045pt}\or \vspace{-1.555pt}\or
    \vspace{-2.955pt} \fi

    \ifx \anrede\empty \else \anrede \par \fi\nobreak
    \addtolength\textheight{63mm}
    }
{
    \renewcommand{\baselinestretch}{1.0}
    \ifcase \@ptsize\relax \normalsize \or \small \or \footnotesize \fi

    \vspace{\grussskip} \par \nobreak \stopbreaks \noindent
    \parbox[t]{3.5in}{\raggedright \ignorespaces {\normalsize \ifx
            \gruss\empty \else \gruss \mbox{} \\[13.92mm] \fi \ifx          
            \unterschrift\empty \relax \else \ifklassisch
            \textsl{(\unterschrift)} \else \unterschrift \fi
        \fi}\strut} \ifx \anlagen\empty \else \vspace{4mm} \par \anlagen
    \par \fi \ifx \verteiler\empty \else \ifx \anlagen\empty
    \vspace{4mm}\par \fi \verteiler \fi }

\def\ps@firstpage{
    \renewcommand{\baselinestretch}{1.0}
    \ifcase \@ptsize\relax \normalsize \or \small \or \footnotesize \fi
        \headheight16pt 
    \def\@oddhead{ 
        \unitlength1mm
        \begin{picture}(0,0)
            \put(-9, 3){\parbox{180mm}{\Large \myfont \ifklassisch \textsl{\quad\name}
                    \else \textsc{\quad\name} \fi}}
            \put(-9, 4){\parbox{180mm}{\hfill \normalsize \myfont \ifklassisch \textsl{
                        \begin{tabular}{r} \strasse \quad \\ \ifx \zusatz\empty \else
                            \zusatz \quad \\ \fi \ort \quad \ifx \land\empty \else \\
                            \land \quad \fi \end{tabular}} \else \textsc{
                        \begin{tabular}{r} \strasse \quad \\ \ifx \zusatz\empty \else
                            \zusatz \quad \\ \fi \ort \quad \ifx \land\empty \else \\
                            \land \quad \fi \end{tabular}} \fi }}
            \iftrennlinien 

            \begin{tikzpicture}[remember picture,overlay]

            \path[left color=white,right color=white,middle color=black]
            ([xshift=45pt,yshift=-70pt]current page.north west)+(0,0) rectangle +(185mm,0.5pt); 


            \end{tikzpicture}

            \fi 

            \iflochermarke \put(-20,- 130.50){\line(1,0){4}} \fi
            \iffaltmarken \put(-15,- 75.16){\line(1,0){3}}
            \put(-15,-182.16){\line(1,0){3}} \fi
            \put( -9, -22.00){\makebox(85,4) %-22
                {\scriptsize \ifx \retouradresse\empty
                    \textrm{\name\ $\cdot$\ \strasse\ $\cdot$\ \ort \ifx
                            \land\empty \else \ $\cdot$\ \land \fi } \else
                        \textrm{} \fi}}
            \put(-1,-28.15){\parbox[t]{4in}{\ifx \postvermerk\empty \hfill \par
                    \else \textbf{\postvermerk} \par \vspace{2mm} \fi \adresse}}            
            \iffenstermarken
            \put(-9,-18.15){\line( 1, 0){1}} \put(-9,-18.15){\line( 0,-1){1}}
            \put(76,-18.15){\line(-1, 0){1}} \put(76,-18.15){\line( 0,-1){1}}
            \put(-9,-61.00){\line( 1, 0){1}} \put(-9,-61.00){\line( 0, 1){1}}
            \put(76,-61.00){\line(-1, 0){1}} \put(76,-61.00){\line( 0, 1){1}}
            \fi
        \end{picture}
        \hfill}
    \def\@oddfoot{\unitlength1mm
        \begin{picture}(0,0)
            \put(-9,0){\parbox{180mm}{\footnotesize \myfont 
                    \iftrennlinien                                                                  %trennlinie footer

                    \begin{tikzpicture}[remember picture,overlay]

                    %\if \headsep43mm -790pt \else \if \headsep53mm -780pt  \else -770pt \fi \fi
                    %\if\headsep43mm -790pt\fi \if\headsep53mm -780pt\fi \if\headsep63mm -770pt\fi
                    \path[left color=white,right color=white,middle color=black]
                    ([xshift=45pt,yshift=   \if \headsep43mm -790pt \else -780pt \fi ]current page.north west)+(0,0) rectangle +(185mm,0.5pt); 

                    \end{tikzpicture}               

                    \fi

                    \begin{tabular}{ll}
                        \ifx \telefon\empty \else \telefontex & \telefon \\ \fi \ifx
                        \telefax\empty \else \telefaxtext & \telefax \\ \fi \ifx
                        \telex\empty \else \telextext & \telex \\ \fi \ifx
                        \email\empty \else \emailtext & \email \\ \fi \ifx
                        \http\empty \else \httptext & \http \\ \fi \
                    \end{tabular}\hfill
                    \begin{tabular}{ll}
                        \ifx \bank\empty \else \ifx \blz\empty \else \ifx
                        \konto\empty \else \banktext & \bank \\ & \blztext \space \blz
                        \\ & \kontotext \space \konto \\ \ \fi \fi \fi
                    \end{tabular}}}
        \end{picture} \hfill}
    \def\@evenhead{\@oddhead} \def\@evenfoot{\@oddfoot}}

\def\ps@regularpage{ \headheight36pt\def\@oddhead{\unitlength1mm
        \begin{picture}(0,0)
            \put(-9,3){\makebox(180,15){\normalsize \ifklassisch \textsl{ 
                {\Large\quad}\name\hfill\datum\hfill\pagename\ \thepage\quad}
                \else \textsc{{\Large\quad}\name\hfill\datum\hfill\pagename\
                \thepage\quad} \fi}}
            \if \put(-9,0){\rule{180mm}{0.5pt}} \fi
        \end{picture}\hfill}
    \def\@oddfoot{\empty} \def\@evenhead{\@oddhead}
    \def\@evenfoot{\@oddfoot}}

\IfFileExists{babel.sty}
    {\RequirePackage[\sprache]{babel}[2001/03/01]}
    {\ClassError{g-brief}
        {Babel not installed !!!}
        {Get babel package !!!}}

\ifx \@docencoding\empty \else
\IfFileExists{inputenc.sty}
    {\RequirePackage[\@docencoding]{inputenc}}
    {\ClassError{g-brief}
        {LaTeX2e not complete installed or out of date !!!}
        {Get current LaTeX2e !!!}}
\fi

\IfFileExists{marvosym.sty}
    {\RequirePackage{marvosym}}
    {}
    \def\Telefon#1{\def\telefon{#1}} \def\telefon{}
\IfFileExists{europs.sty}
    {\RequirePackage{europs}}
    {}{}
\IfFileExists{eurosym.sty}
    {\RequirePackage{eurosym}}
    {}{}


\endinput
%%
%% End of file `g-brief.cls'.

Update 2: Thanks y'all! I went for the easiest solution and \ifcase did the trick!

3
  • please edit your example so that people can run the example reproduce the error. Mar 26 '17 at 10:51
  • 1
    if ifeqcase (which you have not defined) does not work by expansion, you would get this error. You would also get it if the argument is not a b or c as Tex would need a number and so give the missing number error before giving the error message from your \PackageError call. Mar 26 '17 at 10:54
  • I think it would be more productive to tell what you want to achieve, instead of asking how to fix a problem that might not even solve what you want to achieve.
    – Johannes_B
    Mar 26 '17 at 11:12
1

The macro \IfEqCase returns the instructions for choosing among the cases and printing the result. It cannot be used in the context you wish to, because \setlength requires an explicit length, not the instructions to produce it.

You need an expandable version:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\setvalues}{mmm}
 { % #1 = symbolic name, #2 = set of values, #3 = default for error
   \cs_new:Npn #1 ##1
    {
     \str_case_x:nnF { ##1 }{ #2 }
      {% invalid case
       #3
       \msg_error:nnn{sam-oht/setvalues}{invalid}{##1}
      }
    }
 }
\msg_new:nnnn{sam-oht/setvalues}{invalid}
 {
  Invalid~choice~'#1'
 }
 {
  You~have~to~supply~a~good~value,~not~'#1'
 }
\ExplSyntaxOff

\setvalues{\headsepValue}{
  {a}{43mm}% \headsep{43mm}
  {b}{53mm}% \headsep{53mm}
  {c}{63mm}% \headsep{63mm}
}{0pt}

\begin{document}

\newcommand{\valueUsedForSpacing}{a}

\setlength{\headsep}{\headsepValue{\valueUsedForSpacing}}

\the\headsep

\setlength{\headsep}{\headsepValue{c}}

\the\headsep

\setlength{\headsep}{\headsepValue{z}}

\the\headsep

\end{document}

enter image description here

Here's the error message on the console

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! sam-oht/setvalues error: "invalid"
! 
! Invalid choice 'z'
! 
! See the sam-oht/setvalues documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

l.43 \setlength{\headsep}{\headsepValue{z}}

? h
|'''''''''''''''''''''''''''''''''''''''''''''''
| You have to supply a good value, not 'z'
|...............................................
? 
1

Here is an example, what you can do using only eTeX primitives:

\newdimen\headsep

\def\headsepvalue#1{\ifcase\numexpr`#1-`a\relax
   43mm\or 
   53mm\or 
   63mm\else 
   0mm\errmessage{Undefined option to headsepvalue: #1}\fi
}

\headsep=\headsepvalue a

\message{\the\headsep} % test of value setting

\end

Note that \ifcase primitive is expandable. You need nothing like \ExplSyntaxOn.

2
  • What if the keys are mnemonic? Like small, medium or large? There's plenty of expandable case switching macros on TUGboat. This one is quite limited.
    – egreg
    Mar 26 '17 at 15:23
  • My post is about this: when you need something simple, like a=45mm, b=53mm, c=63mm, then you can realize this simply by TeX primitive \ifcase. Of course, I am able to write plenty another expanable case switching macros.
    – wipet
    Mar 27 '17 at 6:11

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.