0

I am trying to create a horizontal enumeration environment with 3 items per row. As seen in the example the \ifferstitem conditional seems to turn to TRUE from FALSE for no apparent reason (at least for me). I suspect there is an esoteric procedure concerning the value of conditionals but I have found nothing in the usual TEX books.

A. What is wrong with my code?

B. As I am not proficient with macros, would there be a better way to implement this? ( For example, I feel that \taband command could be avoided )

\documentclass[11pt]{article}
\begin{document}
\newcounter{tabi}\stepcounter{tabi}
\newcounter{tabirow}
\newif \iffirstitem
\firstitemtrue 
 \newcommand{\taband}{&}
 \newcommand{\addand}{\iffirstitem \firstitemfalse TruetoFalse
                 \else\taband \& Added
                 \fi}
%If 3 items in the row then change row and reset tabirow
\newcommand{\hitem}{\ifnum \thetabirow<3  
                                    \addand 
                                    \hbox to 3em{\hfil \roman{tabi}) \thetabirow}
                                    \stepcounter{tabi}
                                    \stepcounter{tabirow}
                \else 
                                    \\[2ex]
                                    \firstitemtrue
                                    \setcounter{tabirow}{0} 
                                    \addand 
                                    \hbox to 3em{\hfil \roman{tabi})         \thetabirow}
                                    \stepcounter{tabi}
                                    \stepcounter{tabirow}
                \fi
           }
\newenvironment{henumerate}[1]
    {    \setcounter{tabi}{1}
        \setcounter{tabirow}{0}
        \begin{tabular}{| l | l | l |}
        \hline
    }
    {\\\hline
     \end{tabular}
}

\begin{henumerate}{5}
\hitem First 
\hitem Second 
\hitem Wrong
\hitem D
\hitem E
\hitem F
\hitem G
\hitem H
\end{henumerate}
\\

\end{document}

enter image description here

2

You are changing the value of the conditional inside a table cell. Table cells in TeX are groups, so local assignment inside them are only active up to the & (or \\)

You can change this by making the assignments \global. Just add \global before every \firstitemtrue and \firstitemfalse:

\documentclass[11pt]{article}
\begin{document}
\newcounter{tabi}\stepcounter{tabi}
\newcounter{tabirow}
\newif \iffirstitem
\global\firstitemtrue 
 \newcommand{\taband}{&}
 \newcommand{\addand}{\iffirstitem \global\firstitemfalse TruetoFalse
                 \else\taband \& Added
                 \fi}
%If 3 items in the row then change row and reset tabirow
\newcommand{\hitem}{\ifnum \thetabirow<3  
                                    \addand 
                                    \hbox to 3em{\hfil \roman{tabi}) \thetabirow}
                                    \stepcounter{tabi}
                                    \stepcounter{tabirow}
                \else 
                                    \\[2ex]
                                    \global\firstitemtrue
                                    \setcounter{tabirow}{0} 
                                    \addand 
                                    \hbox to 3em{\hfil \roman{tabi})         \thetabirow}
                                    \stepcounter{tabi}
                                    \stepcounter{tabirow}
                \fi
           }
\newenvironment{henumerate}[1]
    {    \setcounter{tabi}{1}
        \setcounter{tabirow}{0}
        \begin{tabular}{| l | l | l |}
        \hline
    }
    {\\\hline
     \end{tabular}
}

\begin{henumerate}{5}
\hitem First 
\hitem Second 
\hitem Wrong
\hitem D
\hitem E
\hitem F
\hitem G
\hitem H
\end{henumerate}
\\

\end{document}

The problem which caused your use \taband is typical for dealing with TeX conditionals: There are some restrictions on which code can stand in the then and else blocks. This can be avoided by using bools from etoolbox instead:

\documentclass[11pt]{article}
\usepackage{etoolbox}
\begin{document}
\newcounter{tabi}\stepcounter{tabi}
\newcounter{tabirow}
\newbool{firstitem}
\global\booltrue{firstitem}
\newcommand{\addand}{%
  \ifbool{firstitem}%
    {\global\boolfalse{firstitem}TruetoFalse}%
    {&\& Added}%
}
%If 3 items in the row then change row and reset tabirow
\newcommand{\hitem}{\ifnum \thetabirow<3  
                                    \addand 
                                    \hbox to 3em{\hfil \roman{tabi}) \thetabirow}
                                    \stepcounter{tabi}
                                    \stepcounter{tabirow}
                \else 
                                    \\[2ex]
                                    \global\booltrue{firstitem}
                                    \setcounter{tabirow}{0} 
                                    \addand 
                                    \hbox to 3em{\hfil \roman{tabi})         \thetabirow}
                                    \stepcounter{tabi}
                                    \stepcounter{tabirow}
                \fi
           }
\newenvironment{henumerate}[1]
    {    \setcounter{tabi}{1}
        \setcounter{tabirow}{0}
        \begin{tabular}{| l | l | l |}
        \hline
    }
    {\\\hline
     \end{tabular}
}

\begin{henumerate}{5}
\hitem First 
\hitem Second 
\hitem Wrong
\hitem D
\hitem E
\hitem F
\hitem G
\hitem H
\end{henumerate}
\\

\end{document}
  • I thought that \global has to do only with definitions. – mhtsort Mar 28 '18 at 12:26

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.