2

I use \import command to modulize my chapters. I also use amsthm package to define my own theorem environment.

When I try to compile the main (root) file, I get the following errors:

Command \thm already defined. ...bered,vlined]{algorithm2e}\newtheorem {thm}
Command \lem already defined. ...em {thm}{Theorem}[section]\newtheorem {lem}
Command \cor already defined. ...orem {lem}{Lemma}[section]\newtheorem {cor}
Command \dfn already defined. ...\theoremstyle {definition}\newtheorem {dfn}
Command \rem already defined. ...ion}\theoremstyle {remark}\newtheorem {rem}

I have to redefine them for every subfile because I want to compile and view the output for them independently.

My source files are as follows:

Root:

%%temp.tex
\documentclass[12pt]{report}
\usepackage[subpreambles=true]{standalone}
\usepackage[ruled,linesnumbered,vlined]{algorithm2e}

\usepackage{sectsty}

\chapternumberfont{\LARGE} 
\chaptertitlefont{\Huge}
\usepackage{import}
\usepackage{amsmath,amssymb,amsfonts,amsthm,complexity,tikz,parskip,subcaption}
\usepackage{graphicx}
\usepackage[nottoc]{tocbibind}
\usepackage{setspace}
\setcounter{tocdepth}{1}


\begin{document}

    \chapter{Introduction}
    \import{./}{import1}
    \import{./}{import2}
    

\end{document}

First import:

%!TeX root = import1


\documentclass{article}

\usepackage{amsthm,enumerate,euscript,tikz}
\usepackage[ruled,linesnumbered,vlined]{algorithm2e}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}{Lemma}[section]
\newtheorem{cor}{Corollary}[section]

\theoremstyle{definition}
\newtheorem{dfn}{Definition}

\theoremstyle{remark}
\newtheorem{rem}{Remark}
\begin{document}
    
\subsection{Theorem}
\begin{thm}
Example theorem
\end{thm}
\begin{proof}
    Example proof
\end{proof}
\end{document}

Second import:

%!TeX root = import2


\documentclass{article}

\usepackage{amsthm,enumerate,euscript,tikz}
\usepackage[ruled,linesnumbered,vlined]{algorithm2e}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}{Lemma}[section]
\newtheorem{cor}{Corollary}[section]

\theoremstyle{definition}
\newtheorem{dfn}{Definition}

\theoremstyle{remark}
\newtheorem{rem}{Remark}
\begin{document}
    
\subsection{Theorem}
\begin{lem}
Example lemma
\end{lem}
\begin{proof}
    Example proof
\end{proof}
\end{document}

What causes this error and how can I solve it?

3 Answers 3

1
+50

For what I understand, you want compilable subfiles. Thus you provide full preamble in them. The error arises when compiling the main file, because there are multiple definitions with exact same \newtheorem. This is due to the subpreambles=true option you pass to standalone.

First option

You can test if the different environments/macros are already defined. For instance you coded \newtheorem{thm}{Theorem}[section], which defines \beginthm and \endthm. Instead, use

\ifdefined\beginthm\else
\newtheorem{thm}{Theorem}[section]
\fi

And so on for every newtheorem you defined.

Second option

Add a full preamble in your main file and remove the standalone option. The drawback being you have to take care of the changes in your various preambles…

Third option

Use the subfiles package. You write only one preamble in your main file, and it is shared with all the subfiles, these ones being compilable alone.

Your main.tex is as follow

\documentclass[12pt]{report}
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}{Lemma}[section]
\newtheorem{cor}{Corollary}[section]

\theoremstyle{definition}
\newtheorem{dfn}{Definition}

\theoremstyle{remark}
\newtheorem{rem}{Remark}

% end of shared preamble
\usepackage{subfiles}

\begin{document}
    
    \chapter{Introduction}
    \subfile{./import1}
    \subfile{./import2}
    
\end{document}

And your import.tex files have this structure

\documentclass[main]{subfiles}

\begin{document}
    
    \subsection{Theorem}
    \begin{thm}
        Example theorem
    \end{thm}
    \begin{proof}
        Example proof
    \end{proof}
\end{document}

EDIT

You can have sub-import. For instance you can code the import1.tex as

\documentclass[main]{subfiles}

\begin{document}
    
    \subsection{Theorem}
    \begin{lem}
        Example lemma
    \end{lem}
    \begin{proof}
        Example proof
    \end{proof}
    \subfile{import2}
    \subfile{import3}
\end{document}

but your import3.tex has to share the preamble with the main.tex, not with the import1.tex, ie you have to use \documentclass[main]{subfiles} in all your subfiles.

3
  • Thanks for the detailed explanation. Does this also work if I have subsubfiles? Meaning that can I import another subfile in import1.tex?
    – padawan
    Jan 21, 2021 at 17:56
  • Sure: see the edit at the end of the answer.
    – NBur
    Jan 21, 2021 at 18:06
  • For the First Option, it only worked for me if I used \ifdefined\thm\else... instead of \ifdefined\beginthm\else.
    – Paul Wintz
    Oct 28, 2022 at 9:55
1

Another way to organize a project and to test partial parts of a document is using \input and commenting the (input) of parts you do not want to compile.

The big advantage is that you have the full preamble in a single place, so if you change a definition or add a package you do not have to alter all partial files.

Main.tex

\documentclass[12pt]{report}
\usepackage[subpreambles=true]{standalone}
\usepackage[ruled,linesnumbered,vlined]{algorithm2e}

\usepackage{sectsty}

\chapternumberfont{\LARGE} 
\chaptertitlefont{\Huge}
\usepackage{import}
\usepackage{amsmath,amssymb,amsfonts,amsthm,complexity,tikz,parskip,subcaption}
\usepackage{graphicx}
\usepackage[nottoc]{tocbibind}
\usepackage{setspace}
\setcounter{tocdepth}{1}

\usepackage{amsthm,enumerate,euscript,tikz}
\usepackage[ruled,linesnumbered,vlined]{algorithm2e}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}{Lemma}[section]
\newtheorem{cor}{Corollary}[section]
\theoremstyle{definition}
\newtheorem{dfn}{Definition}

\theoremstyle{remark}
\newtheorem{rem}{Remark}

\begin{document}

\chapter{Introduction}

\input{import1c}

\input{import2c}

\end{document}

import1c.tex

\section{Theorem}
\begin{thm}
Example theorem
\end{thm}
\begin{proof}
    Example proof
\end{proof}

import2c.tex

\section{Theorem}
\begin{lem}
Example lemma
\end{lem}
\begin{proof}
    Example proof
\end{proof}

Note

I changed the \subsection to \section to match the \newtheorem definitions thus getting proper numbering.

t2

2
  • Although this solution works like a charm, it causes a slight inconvenience by having to switch between files constantly while working.
    – padawan
    Jan 17, 2021 at 19:57
  • @padawan The way I work with large documents is to put just all the tables (or figures or equations) in a single document full of inputs and debug one by one. When I am sure of the final configuration, I compile them all to verify that they have the same look (because over time the formats tend to drift). Then I enter each one in the main document in the appropriate place to check the position on the page, the floats, etc. Of course your workflow might be different. By having many different independent files you have to edit and recompile everything just by adding or updating a package Jan 17, 2021 at 20:45
1

There is the subfiles package intended for such purposes, even though you should try \input first as suggested in the other answer, as it is much simpler.

With the subfiles package, you keep the preamble in the main file and load it from the subfiles as well. For details, see the documentation of the subfiles package

% temp.tex
\documentclass[12pt]{report}
%\usepackage[subpreambles=true]{standalone}
\usepackage[ruled,linesnumbered,vlined]{algorithm2e}
\usepackage{sectsty}
\chapternumberfont{\LARGE} 
\chaptertitlefont{\Huge}

\usepackage{amsmath,amssymb,amsfonts,amsthm,complexity,tikz,parskip,subcaption}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}{Lemma}[section]
\newtheorem{cor}{Corollary}[section]
\theoremstyle{definition}
\newtheorem{dfn}{Definition}
\theoremstyle{remark}
\newtheorem{rem}{Remark}

\usepackage{graphicx}
\usepackage[nottoc]{tocbibind}
\usepackage{setspace}
\setcounter{tocdepth}{1}

\usepackage{subfiles}%%%%% <<<<< load subfiles package last
\begin{document}
....
\end{document}
% import1.tex
\documentclass[temp]{subfiles}%%%% <<< Reference to the main file with the preamble
\begin{document}
\subsection{Theorem}
\begin{thm}
Example theorem
\end{thm}
\begin{proof}
    Example proof
\end{proof}
\end{document}

Note: I'm the maintainer of the subfiles package, but I'm not necessarily advocating its use.

You must log in to answer this question.

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