2

I'm using TeXShop and I was wondering if there is a way to line up the "&" in the code for tables in Latex, to make them readable and able to modify them without having to compile permanently.

For example, I have :

test&44&2
test2&24&3

and I want :

test   &   44  &  2
test2  &   24  &  3

I don't know if there is a solution to my problem ?

Thanks in advance !

Rnd
  • 21
  • Welcome to TeX.SE. Have you heard about the tabular environment? – Mico Nov 14 '17 at 17:47
  • Sorry, I'm a newbie in latex and stackexchange ! Yes, I tried to use the tabular environment but I'm not sure how it works? I failed to install the plugin on my mac... – Rnd Nov 14 '17 at 17:56
  • What do you mean by "plugin on my mac"? Have you installed MacTeX? – Mico Nov 14 '17 at 18:04
  • Yes, I have installed MacTex. I have huge econometric tables but it is very difficult to track mistakes for example. That's why I want to line up the "&" – Rnd Nov 14 '17 at 18:10
  • 1
    Does this help you? https://tex.stackexchange.com/a/46601/134144 – leandriis Nov 14 '17 at 18:13
  • Thank you, I was already on this topic but it does not help me. My tables are already in tex format and I am trying to align the "&" in the code to make them more readable without having to compile.

    I don't know how to make this in TexShop so I tried to the Tabular plugin in Vim, but I fail...

    – Rnd Nov 14 '17 at 18:21
  • Forgive the self publicity, but If you use Vim you might like my table filter that is tex-aware. – Thruston Nov 14 '17 at 19:45
  • You can use the tab key... – Dan Nov 14 '17 at 20:01
  • Since vi was mentioned, I'll counter with M-x align-current in Emacs (not sure it it is auctex that provides the tex aware alignments here), really nice feature. Still wonder why other editors don't copy some of these features – daleif Nov 14 '17 at 20:16

1 Answers1

2

I don't know what features texshop has for aligning table columns but if you save those rows it is easy to format them on the commandline and put them back. I think mac os has column

If you save

test&44&2
test2&24&3

as file

then

column -t -s\& -o' & ' file

produces

test  & 44 & 2
test2 & 24 & 3

here -t says to make tabular output, -s\& says to split up the input on & and -o' & ' says to put _&_ between each column after padding them with spaces.

David Carlisle
  • 757,742
  • on my mac ox 10.9.5, the -o option is not recognized. Whatever I try to escape the & as -s argument it disappears in output, only whitespace is used. –  Nov 14 '17 at 20:08
  • @jfbu what shell do you use? singe quotes around space & space work for me in bash, still you could use something else like zzz and fix it up later in the editor – David Carlisle Nov 14 '17 at 20:28
  • I am using bash. Yes, quotes like you use should be fine. The problem is that my version of column (man page date July 29, 2004) simply refuses -o option column: illegal option -- o, usage: column [-tx] [-c columns] [-s sep] [file ...] and has no substitute. –  Nov 14 '17 at 22:21
  • @jfbu $ column -? column: unknown option -- ?

    Usage: column [options] [...]

    Options: -c, --columns width of output in number of characters -t, --table create a table -s, --separator possible table delimiters -o, --output-separator columns separator for table output; default is two spaces -x, --fillrows fill rows before columns

    -h, --help display this help and exit -V, --version output version information and exit

    For more details see column(1).

    – David Carlisle Nov 14 '17 at 22:37
  • my version does not even have -h and does not accept long form options such as --table. –  Nov 14 '17 at 22:47
  • @jfbu I should have stuck to sed :-) – David Carlisle Nov 14 '17 at 22:50
  • as long as you don't use perl :-) –  Nov 14 '17 at 22:51