29

I have a problem adding tabs and I can't use a tabular environments (like tabular or tabbing) because I can't mix it with enumerate environments.

Is there a way to add a simgle tab like in any text editor? I use MathType to make equations writing much easier and there you can insert tabs, so when you export the code you see MathType uses & for that, but when you compile that code, LaTeX complains with:

Misplaced alignment tab character &.

So somehow LaTeX knows that I'm trying to add a tab and it's driving me crazy.

I thought to create my own "tab" macro or command: the idea is to tell LaTeX to go back to the left (or right) margin with \hspace and the move to the "tab" with another \hspace, something like:

Hi everybody. \hspace{-len_to_left_margin} \hspace{tab_value} My name is Agustin.

Is that posible?

5
  • you can not do what you want with spaces (or not without great difficulty) as word space is not fixed so you would have to arrange for your negative hspace to stretch and shrink by the same amount, which is highly variable as tex optimises white space stretching and line breaking over the while paragraph. Your editor is inserting & so is expecting the expressions to be in tabular or align or similar environments. Sep 19, 2012 at 23:14
  • And is there a way to tell LaTeX to move to a certain position inside the line? Something like: "\moveto{5cm}"
    – Agustin
    Sep 19, 2012 at 23:19
  • @Agustin There is a similar question I want to indent the next line by an exactly specified position Sep 19, 2012 at 23:28
  • 1
    you can go blank line to start a paragraph and \noindent\hspace*{5cm} but really this is all wrong you shouldn't be doing this. Only in very rare exceptional cases should you ever see explicit spacing commands in a document, it goes against the whole point of latex. Sep 19, 2012 at 23:30
  • Why can't you use tabbing? Can you add some code to your question showing the problems you have using tabbing? Sep 20, 2012 at 1:50

2 Answers 2

42

If I understand correctly, you are looking for something like this: enter image description here

\documentclass{article}

\newcommand{\itab}[1]{\hspace{0em}\rlap{#1}}
\newcommand{\tab}[1]{\hspace{.2\textwidth}\rlap{#1}}

\begin{document}

\begin{enumerate}
\item \itab{Format:}  \tab{$math$ formula}
      \tab{is} \tab{(the real number)}
\item \itab{Example:} \tab{$\pi$} 
      \tab{is (first digits)} \tab{3.141593}    
\item \itab{Ome more:} \tab{$log_{10}(\pi)$} 
      \tab{is} \tab{1.14473}    
\end{enumerate}
\end{document}

Update

A simpler approach is put the text in a \makebox so we need only one command. In the example, when the text is wider than 25% of the line width, the text turns red (for demostration purposes) and the length of the \makebox is doubled automatically:

mwe

\documentclass{article}
\usepackage{ifthen,xcolor}
\newlength{\tabcont}

\newcommand{\tab}[1]{%
\settowidth{\tabcont}{#1}%
\ifthenelse{\lengthtest{\tabcont < .25\linewidth}}%
{\makebox[.25\linewidth][l]{#1}\ignorespaces}%
{\makebox[.5\linewidth][l]{\color{red} #1}\ignorespaces}%
}%

\begin{document}

\begin{enumerate}

\item \tab{Format:} 
      \tab{$math$ formula}
      \tab{is} 
      \tab{(the real number)}

\item \tab{Example:} 
      \tab{$\pi$} 
      \tab{is (first digits)}
      \tab{3.141593}    

\item \tab{Example:} 
      \tab{$\pi$} 
      \tab{is (more digits)}
      \tab{3.141592653589793238}    

\item \tab{One more example:} 
      \tab{$log_{10}(\pi)$} 
      \tab{is}
      \tab{1.14473}

\end{enumerate}
\end{document}

Update

But there is no need to reinvent the wheel! It could be worth look the tabto package:

\documentclass{article}
\usepackage{tabto}
\begin{document}
\begin{enumerate}
\NumTabs{6}
\item Format: 
      \tab{$math$ formula}
      \tab{is} 
      \tab{(the real number)}
\item Example: 
      \tab{$\pi$} 
      \tab{is (first digits)}
      \tab{3.141593}    
\item Example: 
      \tab{$\pi$} 
      \tab{is (more digits)}
      \tab{3.141592653589793238}    
\item One more example: 
      \tab{$log_{10}(\pi)$} 
      \tab{is}
      \tab{1.14473}
\end{enumerate}
\end{document}

The result is the same as above (without color).

5
  • Really nice, thank. That's what I was looking for.
    – Agustin
    Sep 21, 2012 at 14:37
  • I've used this for a way of adding nice indentation to itemized lists using [custom bullet] formats. Thanks
    – DazBaldwin
    Aug 19, 2014 at 11:31
  • I used this, and when placing a \footnote inside a tab{} it appears the symbol but not the footnote itself, why?.
    – Hans
    Apr 12, 2015 at 20:53
  • @Hans Because is not a good idea include a footnote inside \rlap or \makebox (try \makebox{test\footnote{test}}). However, note that the last solution with tabto package have not this problem.
    – Fran
    Apr 12, 2015 at 21:28
  • @Fran Thanks so much, I "patched" it with a \phantom statement, but thanks again, I wanted an explanation on why it didn't work.
    – Hans
    Apr 12, 2015 at 22:26
7

Here is an option that mimics Heiko's answer in I want to indent the next line by an exactly specified position:

enter image description here

\documentclass{article}
%\usepackage{showframe}% http://ctan.org/pkg/showframe
\usepackage[savepos]{zref}% http://ctan.org/pkg/zref
\makeatletter
% \zsaveposx is defined since 2011/12/05 v2.23 of zref-savepos
\@ifundefined{zsaveposx}{\let\zsaveposx\zsavepos}{}
\newcounter{hposcnt}
\renewcommand*{\thehposcnt}{hpos\number\value{hposcnt}}
\newcommand*{\tab}[2]{% \tab{<len>}{<stuff>}
  \stepcounter{hposcnt}%
  \zsaveposx{\thehposcnt}%
  \zref@refused{\thehposcnt}%
  \kern\dimexpr-\zposx{\thehposcnt}sp+1in+\oddsidemargin\relax%
  \rlap{\kern#1\relax#2}%
  \kern\dimexpr-1in-\oddsidemargin+\zposx{\thehposcnt}sp\relax%
}
\makeatother
\setlength{\parindent}{0pt}% Just for this example
\begin{document}

\makebox[5em][l]{\texttt{5em}\hrulefill}\par
\makebox[25ex][l]{\texttt{25ex}\hrulefill} \par
\makebox[\linewidth][l]{\texttt{\string\linewidth}\hrulefill} \par
Text1 \tab{5em}{Text2} \tab{25ex}{Text3} \tab{\linewidth}{Text4}

\end{document}

It allows you to specify a tab position relative to the left margin of the text block using \tab{<len>}{<stuff>}. Positioning is managed via zref's savepos module to mark the PDF location/length (in scaled points) and then perform the horizontal jumping around via \kerning.

In the image provided, the horizontal tab stops are identified via markers to accentuate the positioning.

You must log in to answer this question.

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