# Minimize effort separate pdf and single pdf versions; ref and url issues

I want to create LaTeX content which consists of many sections. I will either typeset all of the sections together to get one PDF or typeset each one separately. If they are typeset together, then I want to be able to use label/ref to create links. If they are typeset separately, then I get a link to the relevant PDF instead of the links to the appropriate sections.

I have an OK start, but I am not sure of the next steps. Here is the code:

\documentclass{article}
\usepackage{filecontents}
\begin{document}

\begin{filecontents}{a.tex}
\section{a}
\label{a-label}
Text for a
\end{filecontents}

\begin{filecontents}{b.tex}
\section{b}
\label{b-label}
Text for b
\end{filecontents}

\begin{filecontents}{only-a.tex}
\documentclass{article}
\usepackage{hyperref}
\newcommand\mychoose[2]{#2}
\begin{document}
\input a.tex
\end{document}\end
\end{filecontents}

\begin{filecontents}{only-b.tex}
\documentclass{article}
\usepackage{hyperref}
\newcommand\mychoose[2]{#2}
\begin{document}
\input b.tex
\end{document}\end
\end{filecontents}

\begin{filecontents}{together.tex}
\documentclass{article}
\usepackage{url}
\newcommand\mychoose[2]{#1}
\begin{document}
\input a.tex
\input b.tex
\end{document}\end
\end{filecontents}
\end{document}\end


Typeset it once to see the files a.tex, b.tex, only-a.tex, only-b.tex and together.tex.

This allows me to create the document with the two sections (by typesetting together.tex). I can create the document consisting of just section "a" (by typesetting only-a.tex). The url command does not work. Also, there is a lot of typing involved to use the mychoose command as demonstrated above. Suggestions welcome!

NOTES:

1. The files "a.tex" and "b.tex" are similar (and there differences are easy to see).
2. The files "together.tex", "only-a.tex" and "only-b.tex" are similar (and their differences are involve different number of input commands and #1 vs #2).
3. I can create the "only-" documents using a computer program and so am not concerned about the duplication.

Better ideas are welcome.

Thanks, Mark

• Interesting question. There are stray \end comments in the only-... filecontents environments, but they will be ignored, since they occur after \end{document}. You could use a common preamble .sty file for all documents there, even with \begin{filecontents}{mycommon.sty}....\end{filecontents} etc. where you hold some of the commands. – user31729 Jul 5 '15 at 2:04
• Take a look at the package docmute which I used to produce either a master file or individual files. This question with my answer is an example of using 'docmute'. tex.stackexchange.com/questions/245138/… – R. Schumacher Jul 5 '15 at 2:36

In my point of view, the external references should be made with xr-hyper and \href{filename#ext-label}{some-text}, where some text is the same as the filename ('url').

Using a common style file for all (sub)documents reduces the typing and in my point of view the \mychoose command is not needed here.

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{mycommon.sty}
\usepackage{xr-hyper}
\usepackage{hyperref}
\end{filecontents}

\begin{document}

\begin{filecontents}{a.tex}
\section{a}
\label{a-label}
Text for a
\end{filecontents}

\begin{filecontents}{b.tex}
\section{b}
\label{b-label}
Text for b
\end{filecontents}

\begin{filecontents}{only-a.tex}
\documentclass{article}
\usepackage{mycommon}
\begin{document}
\input a.tex
\end{document}
\end{filecontents}

\begin{filecontents}{only-b.tex}
\documentclass{article}
\usepackage{mycommon}
\begin{document}
\input b.tex
\end{document}\end
\end{filecontents}

\begin{filecontents}{together.tex}
\documentclass{article}
\usepackage{mycommon}
\begin{document}
\input a.tex
\input b.tex
\end{document}
\end{filecontents}
\end{document}


Update: With \ifinternalref conditional:

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{mycommon.sty}
\newif\ifinternalref
\internalreffalse % First disable the internal references
\newcommand{\quickref}[2]{%
\ifinternalref%
\cref{#1}%  or just \ref{#1}
\else
\href{#2\##1}{#2}%
\fi%
}%

\usepackage{hyperref}
\usepackage{cleveref} % Last package!
\end{filecontents}

\usepackage{mycommon}

\begin{document}

\begin{filecontents}{a.tex}
\section{a}
\label{a-label}
Text for a

\end{filecontents}

\begin{filecontents}{b.tex}
\section{b}
\label{b-label}
Text for b

$$E = mc^2 \label{einstein}$$

See \quickref{a-label}{only-a.pdf} and this internal reference \cref{einstein}
\end{filecontents}

\begin{filecontents}{only-a.tex}
\documentclass{article}
\usepackage{mycommon}
\begin{document}
\input a.tex
\end{document}
\end{filecontents}

\begin{filecontents}{only-b.tex}
\documentclass{article}
\usepackage{mycommon}
\begin{document}
\input b.tex
\end{document}
\end{filecontents}

\begin{filecontents}{together.tex}
\documentclass{article}
\usepackage{mycommon}
\internalreftrue  % Enable the internal references since all is combined
\begin{document}
\input a.tex
\clearpage % Just for testing
\input b.tex
\end{document}
\end{filecontents}
\end{document}

• Comment: The \url package is of no use here, in my point of view, since it is only meant for formatting url, not for linking. The url command used from url package is comparable to \nolinkurl from hyperref, producing no hyperlink. – user31729 Jul 5 '15 at 2:50
• I didn't know about xr-hyper! This is great. I still had problems with the link, but I also found tex.stackexchange.com/questions/41539/… which answered by question. – Mark S. Jul 5 '15 at 3:06
• @user738419: You don't need it really, only for \ref usage. And it has to be setup still. I am thinking about another solution, perhaps, but not right now. – user31729 Jul 5 '15 at 3:07
• When I typeset the document as one large document, I want to link from the given document (together.pdf) to itself. – Mark S. Jul 5 '15 at 3:09
• @user738419: Yes, that's an issue. I'll think about it – user31729 Jul 5 '15 at 3:11