Nice and clean formatting of LaTeX Table Sourcecode

Format LaTeX Table Sourcecode

The sourcecode of LaTeX tables tends to be a bit messy and difficult to read. This is particularly obvious when tables are generated by Stata’s Estout, e.g. something like:

&\multicolumn{6}{c}{Car type}
Repair Record 1978&\multicolumn{2}{c}{Domestic}&\multicolumn{2}{c}{Foreign}&\multicolumn{2}{c}{Total}\\
 & N& Row Pct& N& Row Pct& N& Row Pct\\
Result 1 & 2& 100.00& 0& 0.00& 2& 100.00\\
Result 2 & 8& 100.00& 0& 0.00& 8& 100.00\\

It is pretty impossible to understand the information of this table by just looking at the sourcecode. The LaTeX Editor WinEDT offers the TeXtab Plugin that formats the sourcecode much nicer so that each column is clearly separated, but you have to manually select the content of the table that you want to format. The result looks something like this:

                   & \multicolumn{6}{c}{Car type}
Repair Record 1978 & \multicolumn{2}{c}{Domestic} & \multicolumn{2}{c}{Foreign} & \multicolumn{2}{c}{Total} \\
                   & N                            & Row Pct                     & N                         & Row Pct & N & Row Pct \\
Result 1           & 2                            & 100.00                      & 0                         & 0.00    & 2 & 100.00  \\
Result 2           & 8                            & 100.00                      & 0                         & 0.00    & 8 & 100.00  \\

A Batch File for automatic Formatting

I want to integrate the sourcecode formatting directly into my Stata-Estout routine so that all tables are automatically formatted. You can do this in a batch file in the following steps:

  1. Download the TeXtab plugin from the WinEDT homepage including the APL2 runtime module. Extract the archive and install the runtime module (“apl2wr20.exe”). A restart may be required.
  2. The file “textfosp.ans” is the script that does the actual formatting. Copy it to an obvious folder, e.g. “C:\tableformat” (if you use another folder you need to adjust it in the Batch file below).
  3. Create an empty Batch file (e.g. tableformat.bat) and copy the following lines into it:
    DIR *.tex /b > tablelist.lst
    for /F "tokens=*" %%A in (tablelist.lst) do (
     "apl2run.exe" -rns LxTxFo:"C:\tableformat\textfosp" -input "'3 & \\ -- \&' '%%A' 'xX'"
    REM del *.B*K
    REM del *.err
    REM del B*K.*
  4. Copy the Batch file to the folder where your tables are stored. For example, all my Stata-Estout tables are generated to the folder “Project A\Stata\tables” and this folder only contains the Estout tables.

The batch does the following:

  • The textfosp script does not allow multiple file inputs (as far as I know), hence the first step is to create a list of all TeX files in that particular folder (“tablelist.lst”).
  • Next we loop through the list of tables line by line, i.e. each table in the list is formatted by the script.
  • The script generates some backup files. The last three lines delete those backup files, but as I don’t want to be responsible for any data loss I have commented them (remove “REM” if you want to delete the backups).

Call the Script in Stata

To execute the script from Stata run the following after you created the tables:

winexec tableformat.bat

Optional: If you are annoyed by the popping up of the command line interface when the batch is executed create a *.vbs file with the following content:

Set WshShell = CreateObject("WScript.Shell" ) 
WshShell.Run chr(34) & "tableformat.bat" & Chr(34), 0 
Set WshShell = Nothing

And run from Stata with:

winexec wscript.exe tableformat.vbs

PS: If you know a better way, e.g. with regular expressions and something like SED, let me know.

Leave a Reply