4

quick question: I copied this code:

\documentclass{article}
\usepackage{amsmath,amssymb,calc}
\def\horizlist#1#2#3{%
  \setcounter{enumi}{0}%
  #3%
  \flushleft
  \newlength\stringlength
  \setlength\stringlength{#2}
  \advance\stringlength by #1 \relax
  \def\item{\hfil\egroup\penalty50 \hfill
  \refstepcounter{enumi}%
  \leavevmode\hbox to \stringlength \bgroup\space(\theenumi)\space}
  \leavevmode\bgroup\hskip 0pt plus -1fill }

\def\endhorizlist{\hfil\egroup\endflushleft}

\begin{document}
\begin{enumerate}
\item blah
    \begin{horizlist}{2cm}{\widthof{null}}{\renewcommand\theenumi{\alph{enumi}}}
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \end{horizlist}
\end{enumerate}
\end{document}

It does what it should do, the only problem being that the last item appears on the right, not adjoint to its predecessor on the left. How can i fix this? To illuminate the problem:

The output is like this:

enter image description here

I want this:

enter image description here

Any Help?

2

You'll need to keep track of the current column. I find it easier (and also more flexible) to do the whole thing by defining the horizlist environment as a tabular. This way you can not only use \item instead of &/\\, but also specify the alignment as desired (the 2nd arg of the horizlist env).

\documentclass{article}
\usepackage{calc}

\newcount\horizlistcount
\newenvironment{horizlist}[2]{%
  \setcounter{enumi}{0}%
  \renewcommand\theenumi{\alph{enumi}}%
  \def\nextsep{}%
  \global\horizlistcount=#1
  \def\item{%
    \nextsep
    \refstepcounter{enumi}(\theenumi)
    \ifnum\horizlistcount=0 \global\horizlistcount=#1 \fi
    \global\advance\horizlistcount-1
    \ifnum\horizlistcount=0 \def\nextsep{\\}\else\def\nextsep{&}\fi
  }%
  \begin{tabular}[t]{*{#1}{#2}}
}{%
  \end{tabular}
}

\begin{document}
\begin{enumerate}
\item blah
    \begin{horizlist}{4}{p{\widthof{null}+2cm}}
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \end{horizlist}
\end{enumerate}
\end{document}

The result

(To get into a new line before the embedded table, just say \\ in front of it, or include it into the definition.)

1

Not fully automated, but does what you need. We introduce a command \noitem which inserts "phantom item". The non-automated thing is that we do not know in advance how many \noitems should be there.

\documentclass{article}
\usepackage{amsmath,amssymb,calc}
\def\horizlist#1#2#3{%
  \setcounter{enumi}{0}%
  #3%
  \flushleft
  \newlength\stringlength
  \setlength\stringlength{#2}
  \advance\stringlength by #1 \relax
  \def\item{\hfil\egroup\penalty50 \hfill
  \refstepcounter{enumi}%
  \leavevmode\hbox to \stringlength \bgroup\space(\theenumi)\space}
  \def\noitem{\hfil\egroup\penalty50 \hfill
  \leavevmode\hbox to \stringlength \bgroup}
  \leavevmode\bgroup\hskip 0pt plus -1fill }

\def\endhorizlist{\hfil\egroup\endflushleft}

\begin{document}
\begin{enumerate}
\item blah
    \begin{horizlist}{2cm}{\widthof{null}}{\renewcommand\theenumi{\alph{enumi}}}
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \item null
    \noitem\noitem
    \end{horizlist}
\end{enumerate}
\end{document}

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.