19

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}
Werner
  • 603,163
ericc
  • 225
  • 5
    Maybe related: http://tex.stackexchange.com/questions/100262/is-there-a-way-to-limit-the-scope-of-usepackage?rq=1 – T. Verron May 06 '13 at 05:10
  • Once you've "used" a package, it's loaded in its entirety (based on the options its given), and you can't un-use it. However, you can load them separately based on a condition. That is, load the one or the other conditionally. Is this what you're after? – Werner May 06 '13 at 05:18
  • Can your method be used in this way: only package A is used in the main texts, while only package B is used in the appendix? – ericc May 06 '13 at 05:22
  • Welcome to TeX.SE. It would be helpful if you composed a complete MWE including \documentclass and the appropriate packages that illustrates how want to use the two packages. In this case it would be ok if your document did not compile, as long it was clear what behavior you are trying to achieve. There is not a generic solution, but depending on exactly what you want to do there are probbaly other ways of achieving the desired result. – Peter Grill May 06 '13 at 05:28
  • @ericc: No, not really, since the packages are incompatible. What functionality is provided by the two separate packages that you mention? Perhaps there is a work-around. We would nee more information to adequately help you. Most certainly involving a MWE showing the use of the two packages you're trying to use together. – Werner May 06 '13 at 05:34
  • I have provided an example. My intention is that, my first \end{document} closes the implementation of package A; then I open another document and start to use package B. – ericc May 06 '13 at 05:37
  • 1
    Two separate documents, individually using the packages that are incompatible is okay. However, it would still be contained a two separate documents. – Werner May 06 '13 at 05:39
  • 1
    @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 06 '13 at 05:42
  • 3
    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 06 '13 at 05:48
  • 4
    Everything after \end{document} is treated as comments. – Peter Grill May 06 '13 at 08:03
  • With a separate cls and a self defined option and the ifthen package, this is possible. – Micha May 06 '13 at 05:06
  • It seems the OP's question is about cancelling the package in the middle of a document, not between two documents. – T. Verron May 06 '13 at 05:29
  • Consider to use packages which are not implying a conflict. This would be better instead of creating a weird construct (... maintenance, time effort when creating this kind of conflict avoidance). – mnemonic May 06 '13 at 06:00
  • Not sure if resetting everything (and reset the tables like IniTeX) help, and it isn't possible to list all defined macros either https://tex.stackexchange.com/questions/228496/assertions-in-latex#comment539011_228496 . There remains the option of fixing the incompatibility of either package manually. – user202729 Jan 06 '22 at 16:50

2 Answers2

20

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.

Charles Stewart
  • 21,014
  • 5
  • 65
  • 121
  • 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 06 '13 at 08: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 06 '13 at 08:27
10

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}
T. Verron
  • 13,552