1

I have the following problem: Using a combination of the subfiles package, 4 files, a subdirectory, and input in the preambles, and where the 'outer' file subfile-imports the 'inner' file in the subdirectory. I can compile the outer file, but when compiling the inner file on its own, I get a command-already-defined error, which it shouldn't as I understand subfiles.

In detail I have:

  • main.tex
\documentclass{book}
\usepackage{subfiles}
\input{preamble}
\input{sub/preamble}
\begin{document}
\testing
\subfile{sub/main}
\end{document}
  • preamble.tex
\newcommand{\testing}{1, 2, and 3}
  • sub/main.tex
\documentclass[../main]{subfiles}
\begin{document}
\helloWorld
\end{document}
  • sub/preamble.tex
\newcommand{\helloWorld}{Hello World}

Compiling main.tex with the latest pdflatex on an up-to-date macos works fine, but when I compile sub/main.tex then I get the error

...
! LaTeX Error: Command \helloWorld already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.1 \newcommand{\helloWorld}{Hello World}
...

I have a feeling that this is somehow related to the import package used by subfiles, but even after debugging for quite a while, I've not been able to find an error. Presently in my project, I've defined a command in sub/preamble.tex and then I check if it's undefined in main.tex before I input sub/preamble.tex.

  • main.tex
\documentclass{book}
\usepackage{subfiles}
\usepackage{ifthen}
\input{preamble}
\ifthenelse{\isundefined\subPreamble}{\input{sub/preamble}}{}
\begin{document}
\testing
\subfile{sub/main}
\end{document}
  • sub/preamble.tex
\newcommand{\subPreamble}{}
\newcommand{\helloWorld}{Hello World}

This solution, although working, seems wrong. Does anyone have a better solution?

Thanks, Jon

7
  • 1
    Maybe just try making the name of the 2 preamble.tex files different and see if the error solves itself as a workaround.
    – user202729
    Jun 22 at 12:53
  • 1
    as @user202729 wrote: your subfile is finding the sub preamble first and so inputs it twice. Rename the main preamble e.g. to mainpreamble.tex. Jun 22 at 13:16
  • The usual approach is to use \include and \includeonly. This allows you to work on one chapter while not recompiling the whole document every time. Jun 22 at 13:36
  • Ahh, you are so right. Subfiles copies the preamble in the outer file to the inner, and due to the name overlap the inner preamble file is reread. It seems that \subfix is the intended solutions, so I am editing my question with the answer. Thanks!
    – Jon
    Jun 22 at 21:22
  • @Jon If possible with your reputation, it'd be better to write the answer as a proper answer. Do you see the text field labeled "Your Answer" below these comments?
    – gernot
    Jun 24 at 17:06

1 Answer 1

1

Thanks to user202729 and Ulrike Fischer for pointing out the problem with overlapping names and Subfiles' copying of preambles with relative paths. My solution is to use \subfix and modify the outer main.tex as follows:

\documentclass{book}
\usepackage{subfiles}
\input{\subfix{preamble}}
\input{\subfix{sub/preamble}}
\begin{document}
\testing
\subfile{sub/main}
\end{document}

Thank you all.

0

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.