# newcommand does not work with \fi

I tried the command \newcommand{\fistudenti}{\fi} but it seems to work properly only in some cases. Here below there are two insances when it works and when it does not:

WORKS:

\documentclass[0pt]{book}
\usepackage[english]{babel}
\def\PentruStudenti{1}
\newcommand{\fistudenti}{\fi}
\begin{document}
\if\PentruStudenti1
show
\fistudenti
\end{document}


But if I put \PentruStudenti{0} it does not work:

\documentclass[0pt]{book}
\usepackage[english]{babel}
\def\PentruStudenti{0}
\newcommand{\fistudenti}{\fi}
\begin{document}
\if\PentruStudenti1
show
\fistudenti
\end{document}


Where do I do the mistake?

-
Welcome to TeX! You can make the code apparent by indenting it with 4 spaces, or by marking the code and pressing the {} button. Furthermore we usually don't supply a thanks in the question as that is implicit by your name tag. Enjoy your stay at TeX! :) –  zeroth Jun 21 '13 at 8:22
I guess you probably wanted to do \let\fistudenti\fi.... –  Claudio Fiandrino Jun 21 '13 at 8:32
Thanks @claudio, this is what I wanted indeed. –  cristian Jun 21 '13 at 8:54
You're welcome :) –  Claudio Fiandrino Jun 21 '13 at 8:57

The conditional primitives \if.., \else and \fi need to be directly visible by TeX and can not be hidden inside macros. If TeX finds a false \if.. clause it looks at all following tokens until it finds a token equal to \else or \fi without expanding macros. If it finds another \if.. conditional it increases an internal counter and will look for the \fi for this conditional before looking for the outer one.

If you have a macro like \newcommand{\fistudenti}{\fi} the \fi is hidden and not found. The same is true for own \if.. macros inside a false clause. Both will work in a true clause because there macros are expanded as normal.

To make your macro work you need to use \let\fistudenti\fi instead, which makes \fistudenti a token identical to \fi. This is also the way used by \newif to define now conditionals. The \xxxtrue and \xxxfalse macros defined by it simply include \let\ifxxx\iftrue or \let\ifxxx\iffalse.

For further reading have a look at the Q&A What is an \if?.

-

The reason why your \fistudenti does not work has been explained in other answers. I'd like to add a different way to cope with your problem.

It seems that your \PentruStudenti is used for delimiting parts that are conditionally included in your document, depending on the meaning of this macro.

While

\if\PentruStudenti1
<material for the students>
\fi


works, you can be better served with a different approach.

## \newif

Define a conditional:

\newif\ifstudentsversion
%\studentsversiontrue    % new conditionals start false

...

\ifstudentsversion
<material for the students>
\else
<optional material not for the students>
\fi


Then you can uncomment the \studentsversiontrue line for getting the same you'd get with your \def\PentruStudenti{1}.

## Macro with arguments

\newif\ifstudentsversion
%\studentsversiontrue    % new conditionals start false
\makeatletter
\newcommand{\studentsversion}{%
\ifstudentsversion
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}

...

\studentsversion{<material for the students>}
{<material for the teacher>}


Again uncommenting the \studentsversiontrue line will switch between printing the first rather than the second argument.

Note that this can go along with the other method. Which one to use in a particular case probably depends on the size of the material.

## Comment character

With a good editor it's not difficult to select a region and add at the beginning of the lines some characters; in this application I choose to prefix the “only students” parts with ^^A.

\documentclass{article}

\newcommand{\showstudents}{\catcode\^^A=9 }
\newcommand{\hidestudents}{\catcode\^^A=14 }
\hidestudents % default

\begin{document}
\showstudents

This material is seen by everybody.

^^A is seen only when so
^^A decided.

And this material always shows.
\end{document}


If the \showstudents line is commented out, the ^^A combination will be seen as a comment character exactly like %. If it's not commented, ^^A will be ignored.

-

The TeXBook explains this situation.

Conditionals. When an \if... is expanded, TeX reads ahead as far as necessary to determine whether the condition is true or false; and if false, it skips ahead (keeping track of \if...\fi nesting) until finding the \else, \or, or \fi that ends the skipped text.

So in your first example the test is true and so it expands the part. In your second example the test is false; and TeX searching a \fi

-