1

A header guard is used in some programming languages to avoid repeatedly importing the same file. Here's one example of this scenario in LaTeX: Let's say we have header1.sty, header2.sty and main.tex in one directory.

The content of header1.sty:

% Define macros

The content of header2.sty:

% Something before this line
\usepackage{header1}
% Makes use of header1 macros

The content of main.tex:

\documentclass{article}
\usepackage{header1,header2}
\begin{document}
% Document body
\end{document}

Then it appears to me that header1 gets imported twice.

Here are my questions:

  1. Can such repeated importing break anything (i.e. lead to compile errors)?
  2. Even if it doesn't break anything, might implementing a header guard accelerate compilation by making the compiler go over less useless code?
  3. Is there an idiomatic way to implement header guards in LaTeX?
  4. Are there existing packages that are known to break when repeatedly imported? What can be done about them, if any?
Long Horn
  • 115
  • 2
    Relevant: https://tex.stackexchange.com/q/79110/208544 and https://tex.stackexchange.com/q/317137/208544 – mbert Jul 13 '23 at 18:54
  • @mbert Thank you for mentioning the sources. It seems then that the issue is moot if we're using \usepackage. I'll try to edit the question to make it specific to \input and \include. – Long Horn Jul 13 '23 at 20:19
  • 2
    If you're not using \usepackage, then you can borrow a trick from the first link, and check if some macro that is unique to the header is already defined. – Teepeemm Jul 13 '23 at 22:04

0 Answers0