2

I personally like the tabbing environment very much to typeset table-like listings without having to worry about linebreaks, pagrebreaks and the like. However, I now encounter a problem: I have a (very long) tabbing environment with entries that vary greatly in length. Setting the "maximum length" as the tabs is not an option, so I disocvered that apparently LaTeX "skips back" to the absolute position of the tab. I would like to have a variant of tabbing that checks whether the current text position is further to the right than the tab position and then does either of the following to things:

  • Just continue typesetting the text at the current curser position, or
  • Insert a linebreak to correctly continue at the intended position.

Here is an MWE:

\documentclass{article}
\pagestyle{empty}
\begin{document}
\begin{tabbing}
  \hspace{3cm}\=\hspace{3cm}\kill
  \textbf{this is}\>ok\\
  \textbf{this is absolutely not}\>ok\\
  \textbf{I would rather imagine it} %\>
                            like this\\
  \textbf{Altnernatively, I would also appreciate it}\\\>
                            like this\\
\end{tabbing}
\end{document}

enter image description here

I don't know if such an environment exists already, or if I would have to code something - and even then, I wouldn't know where to start, because I don't know how to check what is the current position of the cursor.

Any suggestions?

  • Why don't you use a tabular or list environment? – TeXnician Jul 30 '17 at 13:24
  • tabular doesn't handle page breaks nicely, list is not an option because I want more than one tab per line. If that were not the case, I would use description. – carsten Jul 30 '17 at 13:40
  • Do you know about longtable? – TeXnician Jul 30 '17 at 13:45
  • Yes I do, but then I have to deal with the vertical alignment of the individual cells in order to make sure that the baselines in all cells are nicely aligned. Also, I still cannot get the type of "run-in" or "linebreak" behavior that I would like to have AFAIK, because each cell can only break the text inside itself, which would mean that I would need to deal with multirow and somesuch - and my table is REALLY long (auto-generated, thousands of entries) – carsten Jul 30 '17 at 14:10
  • tabbing is really a rather simple (and useless:-) environment that essentially just sticks each cell in a fixed width box like \makebox[3cm]{...} so there is no linebreaking and no warning if the text overprints the following cell as the cells are never over-full as glue is added to allow them to over-print in this way. – David Carlisle Jul 30 '17 at 14:41
0

I came up with a solution that works for me, but it's not very pretty:

\documentclass{article}
\usepackage{calc}
\pagestyle{empty}

\makeatletter
\newlength\tabs@currentpos
\newlength\tabs@itemwidth
\newlength\tabs@endpos
\newlength\tabs@tabpos

\newcommand\tabnl{%
  \setlength{\tabs@currentpos}{0pt}\\
}
\newcommand\tabs@tabto[2]{%
  \setlength\tabs@itemwidth{\widthof{#2}}%
  \setlength\tabs@endpos{\tabs@currentpos+\tabs@itemwidth}%
  \setlength\tabs@tabpos{#1}%
  #2%
  \ifdim\tabs@tabpos>\tabs@endpos%
  \setlength\tabs@currentpos\tabs@tabpos%
  \addtolength{\tabs@tabpos}{-\tabs@endpos}%
  \hspace{\tabs@tabpos}\ignorespaces\else%
  \setlength\tabs@currentpos\tabs@endpos%
  \fi%
}
\newcommand\tabs@@tabto[2]{%
  \setlength\tabs@itemwidth{\widthof{#2}}%
  \setlength\tabs@endpos{\tabs@currentpos+\tabs@itemwidth}%
  \setlength\tabs@tabpos{#1}%
  #2%
  \setlength\tabs@currentpos\tabs@tabpos%
  \ifdim\tabs@tabpos>\tabs@endpos%
  \addtolength{\tabs@tabpos}{-\tabs@endpos}%
  \else%
  \\
  \fi%
  \hspace*{\tabs@tabpos}\ignorespaces%
}    
\def\tabto{\@ifstar\tabs@@tabto\tabs@tabto}
\makeatother

\setlength{\parindent}{0pt}
\begin{document}
\tabto{2cm}{this is} fine\tabnl
\tabto{2cm}{this is absolutely not} ok\tabnl
\tabto{2cm}{I would rather imagine it} \tabto{7cm}{like this} I think\dots\tabnl
\tabto*{2cm}{Altnernatively, I would also appreciate it} \tabto{7cm}{like this} what do you think?\tabnl
\end{document}

solution

There's still some problem in how I deal with the spaces, plus I think it would be nicer if the actual code would a little more similar to how tabbing and tabular are implemented, but I don't know how to do parsing of & and somesuch.

If anyone has a better idea, I'd be very thankful!

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.