2

I have a LaTeX project which I've created using subfiles which contains several nested directories. Each of the directories other than the bottom-most one in the hierarchy contains a main.tex file which imports the main.tex files from the lower folders. An example of what I mean is shown below:

├── main.tex
├── chapter1/
│   ├── main1.tex
│   └── sectiona/
│       └── main1a.tex
└── chapter2/
    ├── main2.tex
    └── sectiona/
        └── main2a.tex

In this example, main.tex would have main1.tex and main2.tex as subfiles, main1.tex would have main1a.tex as a subfile, and main2 would have main2a.tex as a subfile.

I want to have a command which is defined differently in different chapters (in my specific case, I want a \myvector command which is \overrightarrow in one chapter and \mathbf in another chapter).

My first thought was to define the command differently in the preambles to main1.tex and main2.tex, and then define main1a.tex and main2a.tex to be subfiles of main1.tex and main2.tex respectively (with \documentclass[../main1]{subfiles} or \documentclass[../main2]{subfiles}), and then define main1.tex and main2.tex as being subfiles of main.tex (with \documentclass[../main]{subfiles}). However, this gives an error: ! LaTeX Error: Option clash for document class subfiles.. I don't get this error if I have main1a.tex and main2a.tex be subfiles of main.tex directly (with \documentclass[../../main]{subfiles}), but in this case, if I compile main1a.tex or main2a.tex individually, it won't see the command definitions in the preamble of main1.tex or main2.tex.

Is there a way to define a command differently chapter1 and chapter2 without having to define it in every document in these directories?

3
  • 1
    Welcome to TeX.SX! The error that you observe is definitely an error (of subfiles), and I think that it used to work with earlier versions of LaTeX. I will check the issue.
    – gernot
    Dec 4, 2023 at 14:59
  • 3
    The simplest way to define a macro differently is to first do a \providecommand{\McMacro}[1]{} (assuming it takes 1 parameter) which makes sure that the command is defined, and then redefine it as desired \renewcommand*{\McMacro}[1]{}. Jan 2 at 0:38
  • 1
    Can you please provide some dummy files reflecting your structure and said macro? I.e. sth. we can copy, rename and compile the way you do. As it's written now I find it hard to match your situation. Thank you
    – MS-SPO
    Jan 5 at 17:52

1 Answer 1

1

You can try to define command once with some counter check inside, but every chapter you can set counter to new value.

\documentclass{article}
\usepackage{etoolbox}
\newcounter{vectortype}
\setcounter{vectortype}{3}

\newcommand{\myvector}[1]{
\ifnumequal{\value{vectortype}}{1}{%
\overrightarrow{#1}%
}{}
\ifnumequal{\value{vectortype}}{2}{%
\mathbf{#1}%
}{}
\ifnumequal{\value{vectortype}}{3}{%
Ola! #1%
}{}
}

\begin{document}

\(\myvector{AB}\)

\section{Chapter A}
\setcounter{vectortype}{1}

\(\myvector{AB}\)

\section{Chapter B}
\setcounter{vectortype}{2}

\(\myvector{AB}\)

\section{Chapter C}
\setcounter{vectortype}{1}

\(\myvector{AB}\)

\end{document}

You must log in to answer this question.

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