在estout命令生成的文件中添加TeX代码

时间:2018-06-08 07:38:10

标签: stata tex

我正在使用社区提供的 estout命令生成带有回归结果的TeX个文件,我想知道是否可以添加其他TeX这些文件的行。

更具体地说,我想在生成的文件的开头添加以下行:

\documentclass[leqno,11pt]{article}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}

我还想在文件末尾添加以下行:

\end{document}

我问这个是因为我将重复生成相同的表,并且我想在每次运行回归时查看已编译的pdf而不是TeX代码。

社区贡献的命令tabout具有我上面描述的选项。

estout是否有类似选项?

1 个答案:

答案 0 :(得分:2)

获得所需内容的一种方法是编写一个使用file命令创建复合标记文档的程序:

program define mytex
version 14

local texfile "`1'"

tempname myreadfile mywritefile

file open `mywritefile' using "new_`texfile'", write replace text

file write `mywritefile' "\documentclass[leqno,11pt]{article}" _newline
file write `mywritefile' "\usepackage{booktabs}" _newline
file write `mywritefile' "\usepackage{tabularx}" _newline
file write `mywritefile' "\begin{document}" _newline
file write `mywritefile' _newline

file close `mywritefile'

file open `myreadfile' using "`texfile'", read text
file open `mywritefile' using "new_`texfile'", write append text

file read `myreadfile' line
file write `mywritefile' `"`line'"' _newline

while r(eof) == 0 {
    file read `myreadfile' line
    file write `mywritefile' `"`line'"' _newline
}

file close `myreadfile'
file close `mywritefile'

file open `mywritefile' using "new_`texfile'", write append text
file write `mywritefile' "\end{document}" _newline
file close `mywritefile'

end

现在假设您有一个estout - 生成TeX文件example.tex,其中包含以下内容:

. type example.tex

\title{Introduction to \LaTeX{}}
\author{Author's Name}

\maketitle

\begin{abstract}
The abstract text goes here.
\end{abstract}

\section{Introduction}
Here is the text of your introduction.

\begin{equation}
    \label{simple_equation}
    \alpha = \sqrt{ \beta }
\end{equation}

\subsection{Subsection Heading Here}
Write your subsection text here.

\section{Conclusion}
Write your conclusion here.

通过运行这个小程序,您将获得一个文件new_example.tex

. mytex example.tex

. type new_example.tex  
\documentclass[leqno,11pt]{article}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}


\title{Introduction to \LaTeX{}}
\author{Author's Name}

\maketitle

\begin{abstract}
The abstract text goes here.
\end{abstract}

\section{Introduction}
Here is the text of your introduction.

\begin{equation}
    \label{simple_equation}
    \alpha = \sqrt{ \beta }
\end{equation}

\subsection{Subsection Heading Here}
Write your subsection text here.

\section{Conclusion}
Write your conclusion here.


\end{document}

TeX文件(来自estout)的内容当然可以是TeX加注。

修改

正如OP现在在评论中提到的那样,estout具有preheadpostfoot选项,用于在生成的表之前和之后添加文本。这些似乎支持TeX标记。

但是,此处介绍的解决方案更灵活(例如,对于多行),可扩展,当然它可以超出estout