2

How to align all the $\rightarrow$s so that that all come in a line and not in a zig zag manner?

The MWE:

\documentclass{article}
\usepackage[english]{babel}
\begin{document}
\begin{enumerate}
\item hello $\rightarrow$ world
\item sample $\rightarrow$ test
\item Mathematics $\rightarrow$ Physics
\end{enumerate}
\end{document} 

enter image description here

Werner
  • 603,163
subham soni
  • 9,673
  • What about the align environment (\usepackage{mathtools} is needed for that) –  Dec 27 '14 at 17:22
  • Can you be more detailed @ChristianHupfer – subham soni Dec 27 '14 at 17:23
  • @subhamsoni: Use \leavevmode\rlap{hello}\phantom{Mathematics} and \leavevmode\rlap{sample}\phantom{Mathematics}. – Werner Dec 27 '14 at 17:41
  • I don't see this really as a duplicate of the mentioned post, as the linked answers don't really stick to the fact that this is a list (which breaks across the page boundary). – Werner Dec 27 '14 at 17:53
  • @Werner I'm not seeing the difference yet. I made a few dozen copies of the three \item commands, including $\rightarrow$ symbols, and the larger list broke across pages and stayed aligned. – Mike Renfro Dec 27 '14 at 18:03
  • @MikeRenfro: None of the linked answers use overlapping or tabbing like here. Also the specified link seem to be focussed on a solid structure (like an array, despite some using lists) and using a different horizontal alignment. Maybe I was just eager to put an answer here... – Werner Dec 27 '14 at 18:06
  • @Werner Fair enough. Voted to reopen. – Mike Renfro Dec 27 '14 at 18:08

1 Answers1

5

You can specify the widest entry

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tabto}

\newenvironment{tenumerate}[1]
 {\settowidth{\dimen0}{#1 }% trailing space
  \TabPositions{\dimen0}%
  \enumerate}
 {\endenumerate}

\begin{document}

\begin{tenumerate}{Mathematics}
\item hello\tab $\rightarrow$ world
\item sample\tab $\rightarrow$ test
\item Mathematics\tab $\rightarrow$ Physics
\end{tenumerate}

\end{document}

enter image description here

Without specifying the widest entry, passing twice over the environment is necessary.

Here's a way using the .aux file

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tabto}

\newcounter{tenumerate}
\newlength{\tenumtab}

\newenvironment{tenumerate}
 {\stepcounter{tenumerate}%
  \ifcsname tenumerate@\romannumeral\value{tenumerate}\endcsname
    \setlength{\tenumtab}{\csname tenumerate@\romannumeral\value{tenumerate}\endcsname}%
  \else
    \setlength{\tenumtab}{0pt}%
  \fi
  \edef\currenttenumtab{\the\tenumtab}%
  \TabPositions{\tenumtab}%
  \enumerate}
 {\writetenumtab\endenumerate}
\newcommand{\titem}[1]{%
  \settowidth{\dimen0}{#1 }%
  \ifdim\dimen0>\tenumtab \setlength{\tenumtab}{\dimen0}\fi
  \item #1\tab\ignorespaces
}
\makeatletter
\def\writetenumtab{%
  \immediate\write\@auxout{%
    \global\string\@namedef{tenumerate@\romannumeral\value{tenumerate}}{\the\tenumtab}%
  }%
  \ifdim\currenttenumtab=\tenumtab
  \else
    \@latex@warning{Tab position for `tenumerate' changed}%
  \fi
}
\makeatother


\begin{document}

\begin{tenumerate}
\titem{hello} $\rightarrow$ world
\titem{sample} $\rightarrow$ test
\titem{Mathematics} $\rightarrow$ Physics
\end{tenumerate}

\begin{tenumerate}
\titem{A} XXX
\titem{B} YYY
\titem{CCC} ZZZ
\end{tenumerate}

\end{document}

You're warned if the tab position changes so you need another LaTeX run.

egreg
  • 1,121,712