### Displaying Matrices in LaTeX

Program outtable creates LaTeX code for a matrix. You can use Stata's findit command to download the program from the internet. Please see our webpage on how to use findit to search for programs and additional help for more details. outtable creates a LaTeX file containing the LaTeX code for the matrix to be displayed. The LaTeX file can then be included in a LaTeX file in the place where the matrix is to be displayed.

Example 1: Displaying a variance-covariance matrix after regression

use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear
(highschool and beyond (200 cases))
regress write math female read
      Source |       SS       df       MS              Number of obs =     200
-------------+------------------------------           F(  3,   196) =   72.52
Model |  9405.34864     3  3135.11621           Prob > F      =  0.0000
Residual |  8473.52636   196  43.2322773           R-squared     =  0.5261
-------------+------------------------------           Adj R-squared =  0.5188
Total |   17878.875   199   89.843593           Root MSE      =  6.5751
------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
math |   .3974826   .0664037     5.99   0.000      .266525    .5284401
female |    5.44337   .9349987     5.82   0.000      3.59942    7.287319
read |   .3252389   .0607348     5.36   0.000     .2054613    .4450166
_cons |   11.89566   2.862845     4.16   0.000     6.249728     17.5416
------------------------------------------------------------------------------
matrix v=e(V)
outtable using table1, mat(v)

Here is the code contained in LaTeX file table1.tex generated by the command above.

% matrix: v file: table1.tex  16 Dec 2003 11:35:20
\begin{table}[htbp]
\begin{tabular}{|l|l|l|l|l|}\hline
& math  & female  & read  & cons  \\ \hline
math & .00440946 \\ \hline
female & -.00048283 & .87422256 \\ \hline
read & -.00266962 & .00255177 & .00368872 \\ \hline
cons & -.09243854 & -.58431153 & -.05351035 & 8.195884 \\ \hline
\end{tabular}
\end{table}

We can then include the file in our a LaTeX file and compile it. Here is the pdf file showing the table.

Example 2: Displaying both the regression result and the variance-covariance matrix in LaTeX style

We can use command outtex to generate LaTeX code for the regression output and command outtable to create a LaTeX file containing code for the variance-covariance matrix. We will make use some of the options from outtable to display the matrix in a nicer format.

regress write math female read
      Source |       SS       df       MS              Number of obs =     200
-------------+------------------------------           F(  3,   196) =   72.52
Model |  9405.34864     3  3135.11621           Prob > F      =  0.0000
Residual |  8473.52636   196  43.2322773           R-squared     =  0.5261
-------------+------------------------------           Adj R-squared =  0.5188
Total |   17878.875   199   89.843593           Root MSE      =  6.5751
------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
math |   .3974826   .0664037     5.99   0.000      .266525    .5284401
female |    5.44337   .9349987     5.82   0.000      3.59942    7.287319
read |   .3252389   .0607348     5.36   0.000     .2054613    .4450166
_cons |   11.89566   2.862845     4.16   0.000     6.249728     17.5416
------------------------------------------------------------------------------
outtex
%------- Begin LaTeX code -------%
{
\begin{table}[htbp]\centering
\caption{Estimation results : regress
\label{tabresult regress}}
\begin{tabular}{l c c }\hline\hline
\multicolumn{1}{c}
{\textbf{Variable}}
& {\textbf{Coefficient}}  & \textbf{(Std. Err.)} \\ \hline
math  &  0.397  & (0.066)\\
female  &  5.443  & (0.935)\\
read  &  0.325  & (0.061)\\
Intercept  &  11.896  & (2.863)\\
\hline
\end{tabular}
\end{table}
}
%------- End LaTeX code -------%
matrix v=e(V)
outtable using table2, mat(v) nobox caption("Variance-Covariance Matrix") format(%6.4f)

We can then copy and paste the LaTeX code above and include table2.tex created by outtable in a LaTeX file and compile it. Here is the pdf file showing the output in LaTeX format.

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.