TransWikia.com

I get "already defined" error when using `amsthm` package with `import`

TeX - LaTeX Asked on June 28, 2021

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

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

ifdefinedbeginthmelse
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.

Correct answer by NBur on June 28, 2021

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

Answered by Simon Dispa on June 28, 2021

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.

Answered by gernot on June 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP