2
votes

I am trying to export some regression tables from Stata to LaTeX.

The problem is that I want to display variable labels in the table but some of my labels contain the & character. As such, when I use the community-contributed command esttab to export to LaTeX and then try to compile, I get an error because the Texmaker thinks & should indicate an extra column.

Below is my Stata code:

esttab results1 results2 using "$repodir/output/tables/tract_xregs.tex", ///
       se noconstant label star(* 0.10 ** 0.05 *** 0.01) replace  ///
       nonotes compress nomtitles booktabs ///
       s(modelsample modelobs, label("Sample" "N")) 

esttab sumstats1 using "$repodir/output/tables/tract_sumstats.tex", booktabs label ///
       nonumbers cells("mean p50 min max sd") replace 

How can I include the & character in my variable label without getting an error when compiling it?

1
Welcome to Stack Overflow. It is important that you always provide an example with both data and code that reproduces your problem. Please read the Stata tag wiki for advice on how to ask Stata-related questions on here.user8682794

1 Answers

2
votes

Consider the following example using Stata' toy auto dataset, which reproduces your problem:

sysuse auto, clear
estimates clear

label variable weight "One & Two"

regress price weight
estimates store ols

esttab ols, label tex

{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{c}}
\hline\hline
                    &\multicolumn{1}{c}{(1)}\\
                    &\multicolumn{1}{c}{Price}\\
\hline
One & Two           &       2.044\sym{***}\\
                    &      (5.42)         \\
[1em]
Constant            &      -6.707         \\
                    &     (-0.01)         \\
\hline
Observations        &          74         \\
\hline\hline
\multicolumn{2}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{2}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
}

You need to include a \ before the & in the variable label as follows:

label variable weight "One \& Two"

esttab ols, label tex

{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{c}}
\hline\hline
                    &\multicolumn{1}{c}{(1)}\\
                    &\multicolumn{1}{c}{Price}\\
\hline
One \& Two          &       2.044\sym{***}\\
                    &      (5.42)         \\
[1em]
Constant            &      -6.707         \\
                    &     (-0.01)         \\
\hline
Observations        &          74         \\
\hline\hline
\multicolumn{2}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{2}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
}

The former produces an error, while the latter compiles fine.