2

I am creating two documents: 1 is a set of homeworks and the other is their solutions. Each homework and solution set is its own .tex file and there is a master .tex for the HW-booklet and another for the Answers-booklet. I'd like the flexibility so that I can change the order of the homework assignments and the same will be done "automatically" in the answers document.

I am using \include to insert each assignment and right now I have something like

\include{Ass1}

\include{Ass2}

\include{Ass3}

and then in the answers I need to write

\include{Ans1}

\include{Ans2}

\include{Ans3}

Is there a way to have a third file (something like a make file) where I choose the order like 3,1,2 and then each document (the homework and the answers) both use this ordering to include their files in the "right" order so all I have to do is recompile their main .tex files? I do know how to make make files (ha!) so maybe an easy to follow example could help.

  • Welcome to TeX.SX! Are the \include statements consecutively, i.e. nothing else between? Are the names Ass1, Ass2, etc. i.e. with this prefix? – user31729 May 25 '16 at 20:31
  • have you considered making just a single "driver" file that selects one or the other option (problem or solution) to choose which "item" file to input depending on a switch set at the beginning? – barbara beeton May 25 '16 at 20:39
  • I'm flexible with respect to how the include statements are written, currently they are consecutive. – user3774215 May 27 '16 at 16:21
4

This solution relies on a naming scheme both for assignment and answer files. It fetches the external AssX.tex in the specified order with \include and saves the order to a \clist variable.

At any place later on, the \getallsolutions command replaces the prefix given as the first argument with the (assumed) answer prefix, i.e. AssX becomes AnsX then and tries to load those files!

This can be set than in some external file, say, order.tex and input this order.tex instead of changing the main document each time.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\clist_new:N \g_filename_clist
\NewDocumentCommand{\getallquestions}{m}{%
  \clist_gset:Nn \g_filename_clist {#1}
  \clist_map_inline:Nn \g_filename_clist {\include{##1}}
}

\NewDocumentCommand{\getallsolutions}{mm}{%
  \clist_map_inline:Nn \g_filename_clist {
    \tl_set:Nn \l_tmpa_tl {##1}
    \tl_replace_once:Nnn \l_tmpa_tl {#1} {#2}
    \include{\tl_use:N \l_tmpa_tl}
  }
}


\ExplSyntaxOff


\begin{filecontents}{Ass1.tex}
This Ass1
\end{filecontents}

\begin{filecontents}{Ass2.tex}
This Ass2
\end{filecontents}

\begin{filecontents}{Ass3.tex}
This Ass3
\end{filecontents}


\begin{filecontents}{Ans1.tex}
This Ans1
\end{filecontents}

\begin{filecontents}{Ans2.tex}
This Ans2
\end{filecontents}


\begin{filecontents}{Ans3.tex}
This Ans3
\end{filecontents}



\begin{document}

\getallquestions{Ass1, Ass2, Ass3}

\getallsolutions{Ass}{Ans}


Different order:

\getallquestions{Ass3, Ass1, Ass2}

\getallsolutions{Ass}{Ans}


\end{document}

Update -- some improvements

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\clist_new:N \g_filename_clist
\NewDocumentCommand{\getallquestions}{m}{%
  \clist_gset:Nn \g_filename_clist {#1}
  \clist_map_inline:Nn \g_filename_clist {\include{##1}}
}

\NewDocumentCommand{\getallsolutions}{mm}{%
  \clist_map_inline:Nn \g_filename_clist {
    \tl_set:Nn \l_tmpa_tl {##1}
    \tl_replace_once:Nnn \l_tmpa_tl {#1} {#2}
    \include{\tl_use:N \l_tmpa_tl}
  }
}


\ExplSyntaxOff


\begin{filecontents}{Ass1.tex}
This is Ass1
\end{filecontents}

\begin{filecontents}{Ass2.tex}
This is Ass2
\end{filecontents}

\begin{filecontents}{Ass3.tex}
This is Ass3
\end{filecontents}


\begin{filecontents}{Ans1.tex}
This is Ans1
\end{filecontents}

\begin{filecontents}{Ans2.tex}
This is Ans2
\end{filecontents}


\begin{filecontents}{Ans3.tex}
This is Ans3
\end{filecontents}



\begin{document}

\InputIfFileExists{order}{}{}

\getallsolutions{Ass}{Ans}


Different order:

\InputIfFileExists{differentorder}{}{}

\getallsolutions{Ass}{Ans}




\end{document}

order.tex

\getallquestions{Ass1, Ass2, Ass3}

differentorder.tex

\getallquestions{Ass3, Ass1, Ass2}
  • More sophisticated solutions can be made if the settings are settled ;-) – user31729 May 25 '16 at 20:55
  • How can I load the order from said order.tex? – user3774215 May 27 '16 at 16:20
  • @user3774215: See the update please! – user31729 May 27 '16 at 17:03
  • SO MUCH LOVE. This is going to make the next week (and then ensuing years) much easier. – user3774215 May 28 '16 at 1:50
  • @user3774215: If this is the solution, please consider to accept my answer then – user31729 May 28 '16 at 8:16

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.