4

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
See also \mychoose{(\ref{b-label})}{\url{./only-b.pdf}}
\end{filecontents}

\begin{filecontents}{b.tex}
\section{b}
\label{b-label}
Text for b
See also \mychoose{(\ref{a-label})}{\url{./only-a.pdf}}
\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

Mark S.
  • 561
  • 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. –  Jul 05 '15 at 02:04
  • 1
    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'. http://tex.stackexchange.com/questions/245138/is-there-any-way-to-read-arbitrary-part-of-external-tex-file/245146#245146 – R. Schumacher Jul 05 '15 at 02:36

1 Answers1

1

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
See also \href{only-b.pdf#b-label}{only-b.pdf}
\end{filecontents}

\begin{filecontents}{b.tex}
\section{b}
\label{b-label}
Text for b
See also \href{only-a.pdf#a-label}{only-a.pdf}
\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

See also \quickref{b-label}{only-b.pdf} and \quickref{einstein}{only-b.pdf}

\end{filecontents}

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

\begin{equation}
  E = mc^2 \label{einstein}
\end{equation}

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. –  Jul 05 '15 at 02:50
  • I didn't know about xr-hyper! This is great. I still had problems with the link, but I also found http://tex.stackexchange.com/questions/41539/does-hyperref-work-between-two-files which answered by question. – Mark S. Jul 05 '15 at 03: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. –  Jul 05 '15 at 03: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 05 '15 at 03:09
  • @user738419: Yes, that's an issue. I'll think about it –  Jul 05 '15 at 03:11
  • @user738419: Do you need specific references (e.g. Eq. 1.2) when referring to external labels (e.g. to an Equation from only-b.pdf or is **See only-b.pdf` sufficient? –  Jul 05 '15 at 08:09
  • Referring to the file itself is enough for the external references. I am looking for a macro which is like "concatenate". So a macro \cool{xyz} could expand into \href{only-xyz.pdf#a-label}{xyz-a.pdf} and another macro \hot{xyz} expands to \ref{xyz-label}. – Mark S. Jul 07 '15 at 02:40
  • @user738419: Well, not with \cool and \hot, but with \quickref? –  Jul 07 '15 at 02:57
  • Thanks Christian. It is perfect. I will mark it as solved. – Mark S. Jul 08 '15 at 00:54