3

I just have a question if it's possible to insert tabs into one cell in tabularx. For a better illustration of this problem I will include pictures.

TextBox text

This is what I have in textbox in my program. So I can determine where the tab keys, newlines and spaces are. I need same format in cell in tabularx. In LaTeX code I have this:

\begin{tabularx}{\columnwidth}{lX}
\multicolumn{2}{l}{\textbf{Identification:}} \\
\textbf{End A:} & Something \newline Wire 1: asd Color: black Type: copper \newline Wire 2: asd Color: red Type: aluminium \\
\textbf{End B:} &  \\
\end{tabularx}

Latex output

But I need to have the same spacing like in textbox. Is there any possibility how to do it? Because I really dont want to create as many columns as I need for it and then multicolumn those which are not used. Maybe something similar as \> in tabbing.

  • Why not just use extra columns to identify the tab points (see this image)? – Werner Nov 14 '13 at 18:21
  • Maybe I dont uderstand what you mean. How to do it? – Joziff Nov 14 '13 at 18:31
4

If you want a tabular-like alignment, insert columns to suit your needs:

tabular

\documentclass{article}
\begin{document}
\noindent
\begin{tabular}{lll@{\quad}ll@{\quad}ll}
  \multicolumn{7}{l}{\textbf{Identification:}} \\
  \textbf{End A:} & Something \\
    & Wire 1: asd & Color: black & Type: copper \\
    & Wire 2: asd & Color: red   & Type: aluminium \\
  \textbf{End B:} & Something else \\
    & Wire 1: qwe    & Color: green  & Type: silver \\
    & Wire 2: qwerty & Color: yellow & Type: gold
\end{tabular}
\end{document}

If need be, this process can be automated. For example, if you always have Wire X: in the first column, Color: in the second and Type: in the third. It can also be turned into a macro that arranges them accordingly, avoiding you to repeatedly use the alignment operators &.


Alternatively, use a tabbing environment with a number of predefined tabs. You may not use all of them, but that's not a problem:

enter image description here

\documentclass{article}
\newenvironment{tabenv}[1][7em]
  {\begin{tabbing}
   \hspace*{#1}\=\hspace{#1}\=\hspace*{#1}\=\hspace*{#1}\=\hspace*{#1}\=\hspace*{#1}\=\kill}
  {\end{tabbing}}
\begin{document}
\noindent
\begin{tabenv}
  \textbf{Identification:} \\
  \textbf{End A:} \> Something \\
    \> Wire 1: asd \> Color: black \> Type: copper \\
    \> Wire 2: asd \> Color: red   \> Type: aluminium \\
  \textbf{End B:} \> Something else \\
    \> Wire 1: qwe    \> Color: green  \> Type: silver \\
    \> Wire 2: qwerty \> Color: yellow \> Type: gold
\end{tabenv}

\noindent
\begin{tabenv}[8em]
  \textbf{Identification:} \\
  \textbf{End A:} \> Something \\
    \> Wire 1: asd \> Color: black \> Type: copper \\
    \> Wire 2: asd \> Color: red   \> Type: aluminium \\
  \textbf{End B:} \> Something else \\
    \> Wire 1: qwe    \> Color: green  \> Type: silver \\
    \> Wire 2: qwerty \> Color: yellow \> Type: gold
\end{tabenv}
\end{document}

The optional argument supplied to the tabenv environment defines the spaces of the tabs (default is 7em).


Another alternative that treats the list of items like a regular list, putting each in a box of prespecified width, which allows them to wrap to subsequent lines. The list processing is done using etoolbox (other list processing packages will be able to do the same thing), while the tabular arrangement is kept using tabularx so as to fill the entire \linewidth:

enter image description here

\documentclass{article}
\usepackage{etoolbox,tabularx}% http://ctan.org/pkg/{etoolbox,tabularx}
\newlength{\textboxlen}
\newenvironment{tabenv}[1][7em]
  {\setlength{\textboxlen}{#1}%
   \tabularx{\linewidth}{l>{\raggedright\arraybackslash}X}}
  {\endtabularx}
\newcommand{\items}[1]{%
  \renewcommand*{\do}[1]{% Each item will be put in a...
    \makebox[\textboxlen][l]{##1}\hspace{0pt}}% ... box
  \docsvlist{#1}% Process list
}
\begin{document}
\noindent
\begin{tabenv}
  \multicolumn{2}{l}{\textbf{Identification:}} \\
  \textbf{End A:} & Something \\
    & \items{Wire 1: asd, Color: black, Type: copper} \\
    & \items{Wire 2: asd, Color: red, Type: aluminium} \\
  \textbf{End B:} & Something else \\
    & \items{Wire 1: qwe, Color: green, Type: silver} \\
    & \items{Wire 2: qwerty, Color: yellow, Type: gold}
\end{tabenv}

\noindent
\begin{tabenv}[8em]
  \multicolumn{2}{l}{\textbf{Identification:}} \\
  \textbf{End A:} & Something \\
    & \items{Wire 1: asd, Color: black, Type: copper} \\
    & \items{Wire 2: asd, Color: red, Type: aluminium} \\
  \textbf{End B:} & Something else \\
    & \items{Wire 1: qwe, Color: green, Type: silver} \\
    & \items{Wire 2: qwerty, Color: yellow, Type: gold, Stuff: apples, Things: baking, Money: Twelve}
\end{tabenv}
\end{document}
  • This seems to be good solution but problem is that it should not be the same all the time. I mean that the user can hit tabular many times to do more spaces so you dont know how many columns you will need. I can calculate them but I wanted some better solution to create tabular with float columns. Also what mean @{} in columns? – Joziff Nov 14 '13 at 18:42
  • 1
    @MichalJurčík: I've added a tabbing alternative. You can prespecify a number of tab positions, even though you might not use them all. @{<stuff>} is used to identify the intercolumn spacing in a tabular. – Werner Nov 14 '13 at 19:05
  • Perfect and last thing i need to wrap text automatically is it possible? Because the line can be long. – Joziff Nov 14 '13 at 19:12
  • 1
    @MichalJurčík: I've updated the solution to now provide a list-style interface using \items{<item1>,<item2>,...} that should wrap if needed. – Werner Nov 14 '13 at 20:13
  • 1
    @MichalJurčík: Circumventing the inclusion of , requires you to wrap the item in braces: \items{...,{..,..},...}. With all these additional requirements, do you really need tabs to make things align? – Werner Nov 14 '13 at 21:59

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.