1

So I am using the package standalone to combine multiple separate files. In each subfile, I define a new command (its the same in each subfile). The problem is when I used standalone to combine the two subfiles, I get an error relating to redefining the command.

Main file:

\documentclass[class=article,float=true,crop=false, paper=a4,fontsize=11pt,twocolumn]{standalone}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}

\begin{document} \import{1/}{1} \import{2/}{2} \end{document}

Subfile1:

\documentclass[class=article,float=true,crop=false,paper=a4,fontsize=11pt,twocolumn]{standalone}
\usepackage[subpreambles=true]{standalone}
\newcommand{\opt}{\text{OPT}}

\begin{document} Subfile 1 $\opt$ \end{document}

Subfile2:

\documentclass[class=article,float=true,crop=false,paper=a4,fontsize=11pt,twocolumn]{standalone}
\usepackage[subpreambles=true]{standalone}
\newcommand{\opt}{\text{OPT}}

\begin{document} Subfile 2 $\opt$ \end{document}

When compiling, the following error message:

! LaTeX Error: Command \opt already defined.

2 Answers2

2

Approach 1:

As Skillmon already said you can use \providecommand instead of \newcommand.

(With \providecommand attempts at defining the macro in question take place only in case it is not already defined.)

Approach 2:

The standalone-manual says that document-environments of imported files are treated as local scopes/groups.

Therefore you can restrict the definition of \opt to the scope of a document-environment by putting it inside the document-environment. I know you usually don't perform \newcommand inside a document-environment but you can do it:

main.tex

\documentclass[class=article,float=true,crop=false, paper=a4,fontsize=11pt,twocolumn]{standalone}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}

\begin{document} \import{1/}{1} \import{2/}{2} \end{document}

/1/1.tex

\documentclass[class=article,float=true,crop=false,paper=a4,fontsize=11pt,twocolumn]{standalone}
\usepackage[subpreambles=true]{standalone}
\usepackage{amsmath}

\begin{document} % have defining performed inside the document-environment: \newcommand{\opt}{\text{OPT 1}}% Subfile 1 $\opt$ \end{document}

/2/2.tex

\documentclass[class=article,float=true,crop=false,paper=a4,fontsize=11pt,twocolumn]{standalone}
\usepackage[subpreambles=true]{standalone}
\usepackage{amsmath}

\begin{document} % have defining performed inside the document-environment: \newcommand{\opt}{\text{OPT 2}}% Subfile 2 $\opt$ \end{document}

Of course this approach works out only with macros that are not used outside the scope of a document-environment. I.e.: Tokens whose defining took place within a document-environment should not go unexpanded into moving arguments like referencing-labels or section-headings or \captions!

Approach 3:

In situations where all files are to use exactly the same preamble and postamble I don't use packages like standalone or subfiles at all but keep preamble and postamble in separate files and have LaTeX maintain a counter for keeping track of the nesting-level of \input with things that might need a postamble in case the nesting-level of \input is 0.
In case the preamble was already loaded and thus the \documentclass-command was redefined to be equal to \@twoclasseserror, processing preamble.tex is aborted prematurely via \endinput after incrementing the counter for the input-nesting-level.
In case the counter for the input-nesting-level is larger than 0, processing postamble.tex is aborted prematurely via \endinput after decrementing the counter for the input-nesting-level.

./preamble.tex

\expandafter\ifx\csname @twoclasseserror\endcsname\documentclass
  \stepcounter{preambleinputcounter}%
  \expandafter\endinput
\fi
%
% The preamble:
%
\documentclass[a4paper, 11pt,twocolumn]{article}
\newcounter{preambleinputcounter}
% import – establish input relative to a directory
\usepackage{import}
\usepackage{amsmath}
\newcommand{\opt}{\text{OPT}}%
\begin{document}%

./postamble.tex

\ifnum\number\value{preambleinputcounter}>0 %
  \addtocounter{preambleinputcounter}{-1}%
  \expandafter\endinput
\fi
%
% The "postamble":
%
\end{document}%

./main.tex

\input{./preamble.tex}%
\import{./1/}{1}
\import{./2/}{2}
\input{./postamble.tex}%

./1/1.tex

\input{../preamble.tex}%
Subfile 1 $\opt$
\input{../postamble.tex}%

./2/2.tex

\input{../preamble.tex}%
Subfile 2 $\opt$
\input{../postamble.tex}%
Ulrich Diez
  • 28,770
1

I'm not sure what your desired end-result is - if you want to integrate the PDFs generated from Subfile1 and Subfile2, or you're simply splitting your .tex file into multiple sub-files. (I assume you're doing the latter, but correct me if I'm wrong).

It would be much easier on your part if you use the input{} command, as then you only have a single preamble that every .tex file you later combine in your project can access.

As a result, your project would look like:

Main file

\documentclass[class=article,float=true,crop=false, paper=a4,fontsize=11pt,twocolumn]{standalone}
\usepackage[subpreambles=true]{standalone}

% global declaration of command - can be used in any file called by \input \newcommand{\opt}{\text{OPT}}

\begin{document} \input{Subfile1.tex}

\input{Subfile2.tex}

\end{document}

Subfile1.tex (note that these files require no preamble whatsoever)

Subfile 1 $\opt$

Subfile2.tex

Subfile 2 $\opt$

which gives you the result

enter image description here

pikopiko
  • 430
  • But the thing is that I need to subfiles to be compiled separately, so they need a preamble. – Vineet Patel Nov 02 '20 at 01:34
  • Oh, I see. Your problem then is that the command \import doesn't import the compiled PDFs - it imports the .tex code which is why you get your error, as the code seen from the compiler has two \newcommand{} lines. You want to instead import the PDFs into your document, which you can do with a package like pdfpages. – pikopiko Nov 02 '20 at 01:37
  • How would that work in regards with page numbering? Like if the imported pages already have page numbers. – Vineet Patel Nov 02 '20 at 03:46
  • In your case I don't think they would, as you're generating them as standalone documents. Regardless, you could clear the headers and footers (which would remove the page numbers) using a package like fancyhdr. – pikopiko Nov 02 '20 at 07:22