0

I have the following latex file structure (for a good reason) and I would like to use preamble in both main.tex and other_main.tex

|-main.tex
|-subsections
     |-preamble.tex
     |-input1.tex
|-other_main
     |-other_main.tex

However, the issue is that preamble inputs input1.tex. For main.tex to be compiled, preamble.tex needs to use

\input{subsections/input1}

While for other_main.tex to be compiles, preamble.tex needs to use

\input{../subsections/input1}

What I've tried: using versions package with the following files:

preamble.tex

\begin{main}
    \input{subsections/input1}
\end{main}

\begin{other}
    \input{../subsections/input1}
\end{other}

input1.tex

\newcommand{\hi}{hi}

main.tex

\documentclass{article}


\usepackage{versions}
\includeversion{main}
\excludeversion{other}

\input{preamble}

\begin{document}
    \hi
\end{document}

But I'm getting ! Undefined control sequence. \hi error.


I could make a copy of preamble.tex with the different input location, but that is not desirable as the contents of preamble may change over time.

Any idea how to tackle this problem? Thanks in advance!

1 Answer 1

3

the paths are relative to the working directory of latex, not to the document so one possibility is to process the second document from the same working directory but use

pdflatex other-main/other-main.tex

Then you need the same path for both.

Alternatively (and better as it is more portable) is to just use \input{preamble} (or better rename the file to preamble.sty and use \usepackage{preamble}.

Then arrange that preamble.tex is in the input path of both documents.

You could do this either by setting the TEXINPUTS environment variable to

/path/to/main//:

where main is the directory containing main.tex and // means recursively search any subdirectory and the trailing : means search all the standard places.

Or simply put preamble.tex in your local texmf treee so it is found anywhere, typically in ~/texmf/tex/latex

3
  • Thanks @DavidCarlisle, would I have to move input1.tex along with preamble if I were to turn preamble into a sty?
    – Tim
    Jan 14, 2020 at 15:10
  • @Tim if I understand the question no. \usepackage{foo} if you ignore package option setting, is just syntactic sugar for \input{foo.sty} Jan 14, 2020 at 15:15
  • thanks so much @DavidCarlisle! I think I got it. Seems like setting TEXINPUTS=/path/to/main//; worked for me on windows. Turns out that automatically includes input1.tex into path so that \input{input1} works for both main and other_main.
    – Tim
    Jan 14, 2020 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .