# How to undo \usepackage?

I would like to use two packages which conflict with each others. I hope I can apply them separately whenever I need and then cancel them so as to solve the problem. Is it possible?

For example, I would like the following works:

\documentclass{article}
\usepackage{A}
\begin{document}
ABC
\end{document}

\documentclass{article}
\usepackage{B}
\begin{document}
EFG
\end{document}

-
Maybe related: tex.stackexchange.com/questions/100262/… – T. Verron May 6 '13 at 5:10
Two separate documents, individually using the packages that are incompatible is okay. However, it would still be contained a two separate documents. – Werner May 6 '13 at 5:39
@Werner: When I use this code in my .tex file and compile, the document ends with "ABC". i.e. It ignores the following "EFG" part. I hope they can be put in the same document, even if starting at a new page is ok. – ericc May 6 '13 at 5:42
I think that Werner's answer is the best possible (i.e. you can't do it) unless you say exactly which packages you're having problems with. Then the question becomes "how do I use both these packages or something like them, together?", which we can probably answer. – Ryan Reich May 6 '13 at 5:48
Everything after \end{document} is treated as comments. – Peter Grill May 6 '13 at 8:03

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.

-
It won't work if you need cross-references between the document and its appendix. A (dirty) work-around could be to compile the document and appendices separately, but in the same document, using \includeonly. – T. Verron May 6 '13 at 8:16
@T.Verron: \includeonly is more capable, but I guess trickier to work with - would you write it up as an answer? I should note what is lost by compiling separately. – Charles Stewart May 6 '13 at 8:27

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}

-
Thanks for your work as well! – ericc May 6 '13 at 11:48

## protected by Henri MenkeJan 7 at 9:30

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.