How to undo \usepackage?

Tex/Latex have what is called monolithic state: you can't cleanly isolate the configuration done by one package from another, which is one of the reasons why incompatibilities are possible. This means there is no way to identify how to undo what changes the \usepackage command has caused. Think of installing software on your machine: if the software makes lots of changes to operating-system -specific files, and doesn't put together an uninstall script, there is no good way to uninstall the software short of reinstalling the operating system. The situation here is similar, so as Werner says, your reversible \usepackage is impossible.

From the main comments, you say: only package A is used in the main texts, while only package B is used in the appendix?

This suggests that the pdfpages package might work for you, if the package conflict really is unsolveable. The workflow would be that you split the document in two, compile the appendices separately and import the PDF output of the appendices at the end of the main document. See Base document page numbers with pdfpages for an explanation of how to get page numbering to work properly in this case.

This is not usually as good a way as working as compiling everything in one document, because Latex will lose whole-document content: it can add numbers to your pages and keep track of how long your appendices are, but it does not put subsections in the table of contents, or keep track of tables and figures, or references. For that, though, there is software that tries to build up this overall picture by trying to join together information from the separate compilation processes (via their .aux files): see T. Verron's example and the newclude package.


This answer is a complement to Charles Stewart's excellent answer. It shows a (dirty and annoying) workaround to the problem you will face if you need some cross-references between the main text and the appendix.

It uses pdfpages, but instead of compiling the main body and the appendix separately, each from its own master file, you compile them both from the same master file.

See the MWE below for an example. The idea is that you run pdflatex to resolve the references on the first part, then on the second part, then you merge the parts using pdfpages.

Note that:

  • in case you have references to the appendix in the main text, you will need extra runs;
  • in case you have a table of contents in your document, you will have to either use \includepdf options to skip a few pages, or print the table of contents only in the last run;
  • the mv command should be replaced with whatever is appropriate on your OS (mv should work on all unix-like, including macOS and linux, but notably excluding windows);
  • you can invoke the renaming command through \write18, but I didn't want to complicate the example;
  • in the same way, (on unix) you can automate this process using a simple script, but you're on your own there;
  • maybe the most important part: in case your package incompatibility is about cross-referencing (\ref, \contentsline, etc), this workaround probably won't work at all.

In either case, I wouldn't advise using such a workflow. There are usually better (package-dependent) ways to resolve the incompatibility.

%% Filename : document.tex
\documentclass{article}

\usepackage{pdfpages}
\usepackage{filecontents}

%% We fake a package incompatibility with \newcommand

%% First run pdflatex (enough times) with the following 4 lines uncommented
% \newcommand{\mycommand}{Foo} % Incompatibility
% \includeonly{main}
% \newcommand{\includemain}{\include{main}}
% \newcommand{\includeappendix}{\include{appendix}}
%% Now run 'mv document.pdf main.pdf'

%% Then comment them and uncomment the next 4 lines
% \newcommand{\mycommand}{Bar} % Incompatibility
% \includeonly{appendix}
% \newcommand{\includemain}{\include{main}}
% \newcommand{\includeappendix}{\include{appendix}}
%% Then run 'mv document.pdf appendix.pdf'

%% Then comment all and uncomment these 3 lines:
% \includeonly{}
% \newcommand{\includemain}{\includepdf{main.pdf}}
% \newcommand{\includeappendix}{\includepdf{appendix.pdf}}

\begin{filecontents}{main.tex}
  \section{Section}
  \label{sec:1}
  \mycommand
\end{filecontents}

\begin{filecontents}{appendix.tex}
  See section \ref{sec:1}.
  \mycommand
\end{filecontents}

\begin{document}

\includemain

\includeappendix

\end{document}