我正在写我的个人陈述,我想有一个共享的“主要”部分和一个“学校特定”部分。我计划将学校特定部分存储在以下文档中school1.tex
:school2.tex
...
我想\input{school1.tex}
从一开始就main.tex
知道在哪里填充:
- 学校具体部分(在文档末尾)
- 自定义新命令
\newcommand{\school{school1name}}
或变量,如\def \school {school1name}
文档开头的标题
当前的问题:
- 现在我
\input{}
在school1.tex
适当的位置,main.tex
但我希望它位于main.tex
- 我无法引用
\school
在中定义的命令和变量school1.tex
,也无法在标题中填充它们main.tex
我使用\input
和\def
的方式正确吗?有没有更好的方法建议?
答案1
有可能的
- 让 LaTeX 在特定点停止读取输入文件。
- 多次输入一个输入文件,例如,检查在前一次输入期间是否定义了某些占位符宏。
看来您希望对学校特定的 .tex 文件进行编号。
使用以下模板,您只需要在 main.tex 的序言中修改宏,\schoolnumber
以便将其扩展为您将要创建文档的学校的编号(—您可以\schoolname
相应地定义宏,而不是对学校进行编号,并且在序言中和文档结束之前都这样做\input\schoolname.tex
):
文件:main.tex:
\documentclass{article}
%\usepackage...
%...
\newcommand\schoolnumber{1}%
%\newcommand\schoolnumber{2}%
\input school\schoolnumber.tex
% After \input-ting school<k>.tex the first time,
% , the school-variables for school <k> are available.
\begin{document}
\tableofcontents
\section{A section of the general part}
This is a section of the general part.
Now let's within the general part use the school-specific-variables:
\begin{itemize}
\item \verb|\schoolvariableA| is: \schoolvariableA
\item \verb|\schoolvariableB| is: \schoolvariableB
\item \verb|\schoolvariableC| is: \schoolvariableC
\end{itemize}
\section{Another section of the general part}
This is another section of the general part.
Now let's come to the school-specific text-part:
% \input-ting school<k>.tex the second time
% delivers that part of the document-text
% that is specific to school <k>.
\input school\schoolnumber.tex
\end{document}
文件:school1.tex:
% In case the school-variables are not yet defined
% define them and stop reading - we have this case
% when inputting school1.tex the first time.
\ifx\schoolvariableA\UndeFIned
\makeatletter
\newcommand\schoolvariableA{%
This is school-variable A with school 1
}%
\newcommand\schoolvariableB{%
This is school-variable B with school 1
}%
\newcommand\schoolvariableC{%
This is school-variable C with school 1
}%
%...
\makeatother
\expandafter\endinput
\fi
% When LaTeX gets to reading this, the \endinput
% from above was not carried out. This in turn
% implies that school1.tex is not input the first time.
% Thus now deliver the school-specific section:
\section{A section about school 1}
School 1 is a nice school.
School 1 is a nice school.
School 1 is a nice school.
School 1 is a nice school.
School 1 is a nice school.
School 1 is a nice school.
School 1 is a nice school.
That's the end of the school-specific part with school 1.
\endinput
文件:school2.tex:
% In case the school-variables are not yet defined
% define them and stop reading - we have this case
% when inputting school2.tex the first time.
\ifx\schoolvariableA\UndeFIned
\makeatletter
\newcommand\schoolvariableA{%
This is school-variable A with school 2
}%
\newcommand\schoolvariableB{%
This is school-variable B with school 2
}%
\newcommand\schoolvariableC{%
This is school-variable C with school 2
}%
%...
\makeatother
\expandafter\endinput
\fi
% When LaTeX gets to reading this, the \endinput
% from above was not carried out. This in turn
% implies that school2.tex is not input the first time.
% Thus now deliver the school-specific section:
\section{A section about school 2}
School 2 is a strange school.
School 2 is a strange school.
School 2 is a strange school.
School 2 is a strange school.
School 2 is a strange school.
School 2 is a strange school.
School 2 is a strange school.
That's the end of the school-specific part with school 2.
\endinput
当编译 main.tex 时,如果宏\schoolnumber
被定义为扩展为1
,我得到的 main.pdf 如下所示:
当编译 main.tex 时,如果宏\schoolnumber
被定义为扩展为2
,我得到的 main.pdf 如下所示:
答案2
如果你不想在不同的文件中维护不同学校的代码,文档条包你可能会感兴趣
使用该包,您可以获取 .tex 输入文件的标记部分,并将这些部分合并到其他 .tex 输入文件中。
以下示例:所有学校,docstrip 包用于生成文件霍格沃茨.tex,星际舰队学院.tex,米斯蒂克瀑布大学.tex 和EmpireStateUniversity.tex从所有学校。
每个生成的文件都包含该学校的可编译文档。
您可以使用与创建这些 .tex 文件的相同循环,通过\write18
调用 pdflatex 或 texify 或 latexmk 并进行编译。
文件:allschools.tex
%<*ignore>
\input docstrip
\nopreamble\nopostamble
\askforoverwritetrue
%
\def\gobble#1{}%
\def\firstofone#1{#1}%
\def\loopthroughschoolnames#1{%
\ifx\relax#1\expandafter\gobble\else\expandafter\firstofone\fi
{%
\generate{%
\file{#1.tex}{%
\from{allschools.tex}{documentpreamble}%
\from{allschools.tex}{#1-Variables}%
\from{allschools.tex}{documentgeneralparts}%
\from{allschools.tex}{#1-Specific-Sections}%
\from{allschools.tex}{documentend}%
}%
}%
%
% In case you have \write18 available, you can
% now via \write18 call texify or pdflatex or latexmk
% for compiling the file school\number\mycnt.tex
% in order to obtain school\number\mycnt.pdf.
%
\loopthroughschoolnames
}%
}%
%
\loopthroughschoolnames{Hogwarts}
{StarFleetAcademy}
{MysticFallsUniversity}
{EmpireStateUniversity}
\relax
%
\csname stop\endcsname
\bye
%</ignore>
%<*documentpreamble>
\documentclass{article}
%\usepackage...
%...
\makeatletter
%</documentpreamble>
%<*documentgeneralparts>
\makeatother
\begin{document}
\tableofcontents
\section{A section of the general part}
This is a section of the general part.
Now let's within the general part use the school-specific-variables:
\begin{itemize}
\item \verb|\schoolvariableA| is: \schoolvariableA
\item \verb|\schoolvariableB| is: \schoolvariableB
\item \verb|\schoolvariableC| is: \schoolvariableC
\end{itemize}
\section{Another section of the general part}
This is another section of the general part.
Now let's come to the school-specific text-part:
%</documentgeneralparts>
%<*documentend>
\end{document}
%</documentend>
%
%==== School: Hogwarts =================================
%
%<*Hogwarts-Variables>
\newcommand\schoolvariableA{%
This is school-variable A with Hogwarts.
}%
\newcommand\schoolvariableB{%
This is school-variable B with Hogwarts.
}%
\newcommand\schoolvariableC{%
This is school-variable C with Hogwarts.
}%
%...
%</Hogwarts-Variables>
%<*Hogwarts-Specific-Sections>
\section{A section about Hogwarts}
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
That's the end of the school-specific part with Hogwarts.
%</Hogwarts-Specific-Sections>
%
%==== School: StarFleetAcademy =================================
%
%<*StarFleetAcademy-Variables>
\newcommand\schoolvariableA{%
This is school-variable A with Star Fleet Academy.
}%
\newcommand\schoolvariableB{%
This is school-variable B with Star Fleet Academy.
}%
\newcommand\schoolvariableC{%
This is school-variable C with Star Fleet Academy.
}%
%...
%</StarFleetAcademy-Variables>
%<*StarFleetAcademy-Specific-Sections>
\section{A section about Star Fleet Academy}
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
That's the end of the school-specific part with Star Fleet Academy.
%</StarFleetAcademy-Specific-Sections>
%
%==== School: MysticFallsUniversity =================================
%
%<*MysticFallsUniversity-Variables>
\newcommand\schoolvariableA{%
This is school-variable A with Mystic Falls University.
}%
\newcommand\schoolvariableB{%
This is school-variable B with Mystic Falls University.
}%
\newcommand\schoolvariableC{%
This is school-variable C with Mystic Falls University.
}%
%...
%</MysticFallsUniversity-Variables>
%<*MysticFallsUniversity-Specific-Sections>
\section{A section about Mystic Falls University}
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
That's the end of the school-specific part with Mystic Falls University.
%</MysticFallsUniversity-Specific-Sections>
%
%==== School: EmpireStateUniversity =================================
%
%<*EmpireStateUniversity-Variables>
\newcommand\schoolvariableA{%
This is school-variable A with Empire State University.
}%
\newcommand\schoolvariableB{%
This is school-variable B with Empire State University.
}%
\newcommand\schoolvariableC{%
This is school-variable C with Empire State University.
}%
%...
%</EmpireStateUniversity-Variables>
%<*EmpireStateUniversity-Specific-Sections>
\section{A section about Empire State University}
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
That's the end of the school-specific part with Empire State University.
%</EmpireStateUniversity-Specific-Sections>
编译时所有学校使用 (pdf)(La)TeX,您可以获得
A)霍格沃茨.tex
\documentclass{article}
\makeatletter
\newcommand\schoolvariableA{%
This is school-variable A with Hogwarts.
}%
\newcommand\schoolvariableB{%
This is school-variable B with Hogwarts.
}%
\newcommand\schoolvariableC{%
This is school-variable C with Hogwarts.
}%
\makeatother
\begin{document}
\tableofcontents
\section{A section of the general part}
This is a section of the general part.
Now let's within the general part use the school-specific-variables:
\begin{itemize}
\item \verb|\schoolvariableA| is: \schoolvariableA
\item \verb|\schoolvariableB| is: \schoolvariableB
\item \verb|\schoolvariableC| is: \schoolvariableC
\end{itemize}
\section{Another section of the general part}
This is another section of the general part.
Now let's come to the school-specific text-part:
\section{A section about Hogwarts}
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
Hogwarts is a nice school.
That's the end of the school-specific part with Hogwarts.
\end{document}
b)星际舰队学院.tex
\documentclass{article}
\makeatletter
\newcommand\schoolvariableA{%
This is school-variable A with Star Fleet Academy.
}%
\newcommand\schoolvariableB{%
This is school-variable B with Star Fleet Academy.
}%
\newcommand\schoolvariableC{%
This is school-variable C with Star Fleet Academy.
}%
\makeatother
\begin{document}
\tableofcontents
\section{A section of the general part}
This is a section of the general part.
Now let's within the general part use the school-specific-variables:
\begin{itemize}
\item \verb|\schoolvariableA| is: \schoolvariableA
\item \verb|\schoolvariableB| is: \schoolvariableB
\item \verb|\schoolvariableC| is: \schoolvariableC
\end{itemize}
\section{Another section of the general part}
This is another section of the general part.
Now let's come to the school-specific text-part:
\section{A section about Star Fleet Academy}
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
Star Fleet Academy is a strange school.
That's the end of the school-specific part with Star Fleet Academy.
\end{document}
C)米斯蒂克瀑布大学.tex
\documentclass{article}
\makeatletter
\newcommand\schoolvariableA{%
This is school-variable A with Mystic Falls University.
}%
\newcommand\schoolvariableB{%
This is school-variable B with Mystic Falls University.
}%
\newcommand\schoolvariableC{%
This is school-variable C with Mystic Falls University.
}%
\makeatother
\begin{document}
\tableofcontents
\section{A section of the general part}
This is a section of the general part.
Now let's within the general part use the school-specific-variables:
\begin{itemize}
\item \verb|\schoolvariableA| is: \schoolvariableA
\item \verb|\schoolvariableB| is: \schoolvariableB
\item \verb|\schoolvariableC| is: \schoolvariableC
\end{itemize}
\section{Another section of the general part}
This is another section of the general part.
Now let's come to the school-specific text-part:
\section{A section about Mystic Falls University}
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
Mystic Falls University is a funny school.
That's the end of the school-specific part with Mystic Falls University.
\end{document}
d)EmpireStateUniversity.tex
\documentclass{article}
\makeatletter
\newcommand\schoolvariableA{%
This is school-variable A with Empire State University.
}%
\newcommand\schoolvariableB{%
This is school-variable B with Empire State University.
}%
\newcommand\schoolvariableC{%
This is school-variable C with Empire State University.
}%
\makeatother
\begin{document}
\tableofcontents
\section{A section of the general part}
This is a section of the general part.
Now let's within the general part use the school-specific-variables:
\begin{itemize}
\item \verb|\schoolvariableA| is: \schoolvariableA
\item \verb|\schoolvariableB| is: \schoolvariableB
\item \verb|\schoolvariableC| is: \schoolvariableC
\end{itemize}
\section{Another section of the general part}
This is another section of the general part.
Now let's come to the school-specific text-part:
\section{A section about Empire State University}
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
Empire State University is an exciting school.
That's the end of the school-specific part with Empire State University.
\end{document}