0

I try to create a command if...else which seems correct when i expand it in body but not in preamble. The package answers seems to be not include because the \Newassociation command is unknown. I try to use \ifodd directly and it works but il would understand my error (quoting, escaping, ... ?).

Log of pdflatex

! Missing number, treated as zero.
<to be read again> 
               {
l.14 ...tionfiles]{answers}}{\usepackage{answers}}

(/usr/local/texlive/2017/texmf-dist/tex/latex/answers/answers.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/tools/verbatim.sty))

! Undefined control sequence.
l.29 \Newassociation
                {SOL}{Solution}{tempo}

LaTeX code

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}

\newlength{\personnelmm}
\setlength{\personnelmm}{1pt}

\newcommand{\ifpersonnel}[2]
{\ifodd\personnelmm{#1}\else{#2}\fi}

\ifpersonnel{\usepackage[nosolutionfiles]{answers}}{\usepackage{answers}}

% \ifodd\personnelmm
% \usepackage[nosolutionfiles]{answers}
% \typeout{THEN}
% \else
% \typeout{ELSE}
% \usepackage{answers}
% \fi

\Newassociation{SOL}{Solution}{tempo}
\renewcommand{\Solutionlabel}[1]{\textbf{Solution #1}}

\begin{document}
...
4
  • 4
    Personnelmm is a length not a number so running if odd on it makes no sense. Use a counter or explain what you are trying to do. If it is just an on/off switch use an if construction or use a toggle construction from etoolbox
    – daleif
    Commented Oct 22, 2018 at 14:14
  • 2
    note that after \setlength{\personnelmm}{1pt} then \ifodd\personnelmm is false as it will be cast to sp by multiplying by 2^{16}, and whether it is true or false you are using \usepackage inside a local {} group so most of the definitions made by the package will be discarded. Commented Oct 22, 2018 at 14:19
  • For boolean checks/conditionals I like to use etoolbox and its toggles. But you could also use expl3 and its vast array of conditionals and programming tools. Those solution offer a slightly more LaTeX-y syntax and are more natural than using lengths for a true/false switch.
    – moewe
    Commented Oct 22, 2018 at 14:33
  • \setlength{\personnelmm}{1sp} would work; but why not just doing \newcommand{\personnelmm}{1}?
    – egreg
    Commented Oct 22, 2018 at 15:03

2 Answers 2

4

After

\newlength{\personnelmm}
\setlength{\personnelmm}{1pt}

then \personnelmm will be 65536sp and so act as 65536 (even) in a numeric context.

However you do not want a length here at all, simply use

\newif\ifpersonnelmm
\personnelmmtrue % or false

Then your definition is doing either {#1} or {#2} so either way the package is being loaded inside a local group and so all the definitions are being discarded.

The intended definition is

\newcommand{\ifpersonnel}[2]
{\ifpersonnelmm #1\else #2\fi}

However it would be good practice not to name it starting \if... and reserve such names for commands defined via \newif otherwise it is hard to check \if...\else...\fi nesting by eye. (That is it confuses humans, it doesn't confuse the system).

0

Thanks for your help the new version follows and works !

\newif\ifpersonnel
\personneltrue % or \personnelfalse
\newcommand{\sipersonnel}[2]
{\ifpersonnel #1\else #2\fi}

\sipersonnel{\usepackage[nosolutionfiles]{answers}}{\usepackage{answers}}

You must log in to answer this question.

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