7

Suppose I have the following table in my TeXstudio editor:

    Latex & 360 & 101.77 & 10.71 & 101.86 & 64.60 & 127.20  \\ 
    Manufacturing & 360 & -7.33 & 12.59 & -7.24 & -49.00 & 22.00  \\ 
    Cons & 360 & -17.19 & 23.4 & -17.22 & -79.00 & 43.00 \\ 
    Apple and Orange & 360 & 3.38 & 13.84 & 3.60 & -47.00 & 29.00   \\ 
    Services and Harry  & 104 & -4.96 & 20.8 & -4.81 & -57.00 & 30.00   \\ 
    Manchester & 360 & -9.29 & 8.64 & -9.26 & -35.00 & 8.00 \\ 

Sometimes I want to change some numbers in the table. However, it is really really hard to keep tracking which number is behind the other in the table. Is there is any way to align the & behind each other?

  • 1
    I set the default font in my editor to a fixed-width (ttstyle) font, and then, when creating .tex files, I insert spaces as needed during the original text input to achieve whatever editing alignments I wish. – Steven B. Segletes Jun 2 '15 at 12:05
  • @StevenB.Segletes M I know which editor do you use? Could not find (ttstyle) font in TexStudio. – salhin Jun 2 '15 at 12:14
  • 1
    I say "ttstyle" merely to indicate a font with constant width (the name will vary with the font itself). In the TeXworks editor, for example, I use "Lucida Console" font. – Steven B. Segletes Jun 2 '15 at 12:26
  • 4
    @AhmedSalhin Go to Options > Configure TeXstudio > Editor > Font Family. There you can select a fixed-width font. Next to the font dropdown menu is also a "Show Only Monospaced Fonts" checkbox which hides all non-fixed-width fonts from the dropdown menu. Under LaTeX > Manipulate Tables exists a command to “Align Columns” (it is also in the button row at the top of the window). This needs a tabular environment (I guess tabularx works, too) and even checks for column specifications. – Qrrbrbirlbel Jun 2 '15 at 12:26
  • @Qrrbrbirlbel It worked well with tabular and a little messy with tabularx. – salhin Jun 2 '15 at 13:33
6

use latexindent! You can find it here: https://github.com/cmhughes/latexindent.pl

If I have a main.tex like:

\documentclass{article}
\usepackage{siunitx}

\begin{document}    
\begin{tabular}{%
l
S[table-format=3.0]
S[table-format=3.2]
S[table-format=2.2]
S[table-format=3.2]
S[table-format=-2.2]
S[table-format=3.2]
}
Latex & 360 & 101.77 & 10.71 & 101.86 & 64.60 & 127.20  \\ 
Manufacturing & 360 & -7.33 & 12.59 & -7.24 & -49.00 & 22.00  \\ 
Cons & 360 & -17.19 & 23.4 & -17.22 & -79.00 & 43.00 \\ 
Apple and Orange & 360 & 3.38 & 13.84 & 3.60 & -47.00 & 29.00   \\ 
Services and Harry  & 104 & -4.96 & 20.8 & -4.81 & -57.00 & 30.00   \\ 
Manchester & 360 & -9.29 & 8.64 & -9.26 & -35.00 & 8.00 \\ 
\end{tabular}   
\end{document}

and run

latexindent -w main.tex

I get

\documentclass{article}
\usepackage{siunitx}

\begin{document}    
\begin{tabular}{%
        l
        S[table-format=3.0]
        S[table-format=3.2]
        S[table-format=2.2]
        S[table-format=3.2]
        S[table-format=-2.2]
        S[table-format=3.2]
        }
        Latex              & 360 & 101.77 & 10.71 & 101.86 & 64.60  & 127.20 \\ 
        Manufacturing      & 360 & -7.33  & 12.59 & -7.24  & -49.00 & 22.00  \\ 
        Cons               & 360 & -17.19 & 23.4  & -17.22 & -79.00 & 43.00  \\ 
        Apple and Orange   & 360 & 3.38   & 13.84 & 3.60   & -47.00 & 29.00  \\ 
        Services and Harry & 104 & -4.96  & 20.8  & -4.81  & -57.00 & 30.00  \\ 
        Manchester         & 360 & -9.29  & 8.64  & -9.26  & -35.00 & 8.00   \\ 
    \end{tabular}   
\end{document}

in a second.

Please read the manual before, as this is overwriting your input file. The tool gets even nicer in combination with arara. For example:

% arara: indent: {overwrite: yes; trace=true}
  • 2
    latexindent is included in both TeX Live and MikTeX, so it's not necessary to get it from GitHub. – Torbjørn T. Jun 2 '15 at 12:49
  • @TorbjørnT. Oh cool. Thanks for the update. – LaRiFaRi Jun 2 '15 at 12:51
  • 2
    awesome, thanks for advertising latexindent, it brings a smile to my face :) – cmhughes Jun 2 '15 at 15:00
  • 1
    @cmhughes haha, your delight made me smile, too. But I am sorry to have left an issue on your repository... The second last line got one tab too much, imho. – LaRiFaRi Jun 2 '15 at 15:05
  • 1
    no worries, all bug reports gratefully received :) – cmhughes Jun 2 '15 at 15:07
9

In TexStudio you select the data in your table and go to (top of the screen)

LaTeX > Manipulate Tables > Align Columns

This will neatly align your table at the &'s.

  • Welcome to TeX.Stackexchange! – user36296 Apr 25 '17 at 15:29
  • It doesn't work for me, it does reformat the table but with no proper alignment. – Simos Nones Nov 8 '18 at 14:57
1

The way I would handle this example is to insert enough white space in order to generate a table-like view in the editor. The white space is later ignored by latex. Accordingly, an alignment like the following example would be no problem.

Latex         & 360  & 101.77  & 10.71  & 101.86 & 64.60  & 127.20  \\ 
Manufacturing & 360  & -7.33   & 12.59  & -7.24  & -49.00 & 22.00   \\ 
Cons          & 360  & -17.19  & 23.4   & -17.22 & -79.00 & 43.00   \\

If you have very big tables you could also edit them in a spreadsheet and subsequently export as CSV-file with the & as delimiter.

  • Well, that is what I am doing at the moment. However, it is only suitable for small tables like the one in the question (the smallest table in my document). For big tables with different contents' width, this would be a nightmare. – salhin Jun 2 '15 at 12:22

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.