1

I'm writing my statement of purpose, and I want to have a shared "main" part and a "school-specific" part. I plan on storing the school specific parts in documents like school1.tex, school2.tex...

I want to \input{school1.tex} at the beginning and have main.tex know where to populate:

  1. the school specific part (at the end of the doc)
  2. custom newcommands like \newcommand{\school{school1name}} or variables like \def \school {school1name} at the beginning of the doc in a header

Current issues:

  1. Right now I'm \input{}-ing school1.tex in the appropriate location in main.tex but I'd like it to be at the beginning of main.tex
  2. I'm unable to reference \school command and variables defined in school1.tex and populate them in the heading of main.tex

Am I using \input and \def correctly? Any suggestions for a better way of doing this?

  • 1
    Would it work better if you turned the idea on its head? Make school1.tex the complete input file for the school 1 statement, and \input common.tex at the appropriate place (e.g. after \begin{document}. Of course school2.tex will probably have the same structure as school1.tex so you can just copy school1.tex and edit it. – alephzero Sep 15 '18 at 20:47
  • 1
    Am trying to visualize what sort of things would be school specific and how one could intermix them with the other parts. I am reminded of \maketitle which requires you to fill out commands like \title in the preamble. One might use flowfram to add page/location specific additions. – John Kormylo Sep 15 '18 at 21:33
  • @alephzero: yes, thanks! i wanted to avoid copying preambles across multiple docs, but it may be easiest this way – rrrrr Sep 15 '18 at 21:47
  • @JohnKormylo: basically a section at the end that says specific stuff about the school and then a few variables in the main text that reference the school's name – rrrrr Sep 15 '18 at 21:48
  • @rrrrr If you have customized the preamble you could store the customized input in another "common" file and include it in each document, so your "boilerplate" input is just 3 lines, \documentclass{…} \input mystuff.tex \begin{document} . Or you could make your own .sty file which starts with \LoadClass[…]{…} for the original document class, and then includes your customizations. – alephzero Sep 15 '18 at 23:26
3

It is possible

  • to have LaTeX stop reading an input-file at a specific point.
  • to input an input-file several times, hereby, e.g., checking whether some placeholder-macro was defined during a previous input.

Seems you wish your school-specific .tex-files to be numbered.

With the following template you only need to modify within the preamble of main.tex the macro \schoolnumber in order to have it expand to the number of the school for which you are about to create your document(—instead of numbering schools you could define a macro \schoolname accordingly and both in the preamble and before the end of the document do \input\schoolname.tex):

File: 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}

File: 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

File: 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

When compiling main.tex while the macro \schoolnumber is defined to expand to 1, I get main.pdf which looks like this:

enter image description here

When compiling main.tex while the macro \schoolnumber is defined to expand to 2, I get main.pdf which looks like this:

enter image description here

0

In case you don't like to maintain the code for different schools in different files, the docstrip-package might be of interest to you.

With that package you can grab tagged portions of .tex-input-files and combine these portions to other .tex-input-files.

With the following example, allschools.tex, the docstrip-package is used for generating the files Hogwarts.tex, StarFleetAcademy.tex, MysticFallsUniversity.tex and EmpireStateUniversity.tex from allschools.tex.

Each of these generated files contains the compilable document for the school in question.

You could use the same loop that is used for creating these .tex-files for via \write18 calling pdflatex or texify or latexmk and compiling them.

File: 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>

When compiling allschools.tex with (pdf)(La)TeX, you get

a) Hogwarts.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) StarFleetAcademy.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) MysticFallsUniversity.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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.