Suppose you are writing a book that contains problems and you want to be able to create two documents as output: one containing the text of the book and the problems, and one containing the problems and solutions. That seems like a fairly common situation. A sensible format for your file seems to be
\documentclass{book}
\newif\ifsolutions
\newif\iftext
\texttrue
%\solutionstrue
\newtheorem{theorem}{Theorem}
\newenvironment{problem}[1]{\item{\textit{#1}.}}{}
\begin{document}
\chapter{Theory}
\iftext
\begin{theorem}
If \(x\) and \(y\) then \(z\).
\end{theorem}
\fi
\section*{Problems}
\begin{enumerate}
\begin{problem}{Generalization}
Show that Theorem 1 can be generalized by dropping the requirement \(y\).
\ifsolutions
Solution:
First note that
\begin{equation}
a = b.
\end{equation}
The result follows from (1).
\fi
\end{problem}
\end{enumerate}
\end{document}
(For a real book you would probably put the chapters in separate files and use the \include mechanism.)
If you want to include the text and problems, you set \texttrue and if you want to include the problems and solutions you set \solutionstrue.
Now suppose you want to include hyperrefs. All references in the text-and-problems file can be hyperlinks. (Let's assume there are no references in the text to objects in the solutions.) But references in the problems-and-solutions file to objects in the text cannot be hyperlinks --- they have to be plain text --- while references in that file to objects in the problems and solutions should be hyperlinks.
One way to proceed is to (1) use two macros for the references in the problems and solutions, one for references within the problems and solutions and one for references to object in the text, and (2) make available the relevant entries from the aux file for the text-and-problems in the problems-and-solutions file. So something like this:
\documentclass{book}
\usepackage{hyperref}
\def\theoremautorefname{Theorem}
\newif\ifsolutions
\newif\iftext
%\texttrue
\solutionstrue
\iftext
\def\xautoref#1{\autoref{#1}}
\else
\def\xautoref#1{\autoref*{#1}}
\newlabel{theorem}{{1}{1}{Theory}{theorem.1}{}}
\fi
\newtheorem{theorem}{Theorem}
\newenvironment{problem}[1]{\item{\textit{#1}.}}{}
\begin{document}
\chapter{Theory}
\iftext
\begin{theorem}\label{theorem}
If \(x\) and \(y\) then \(z\).
\end{theorem}
\fi
\section*{Problems}
\begin{enumerate}
\begin{problem}{Generalization}
Show that \xautoref{theorem} can be generalized by dropping the requirement \(y\).
\ifsolutions
Solution:
First note that
\begin{equation}\label{equation}
a = b.
\end{equation}
The result follows from \autoref{equation}.
\fi
\end{problem}
\end{enumerate}
\end{document}
That works, but requires the links in the problems and solutions to be coded according to whether they are internal to the problems and solutions or point to the text, and also requires the manual inclusion of the relevant \newlabels from the aux file for the text-and-problems. A better way to proceed would appear be to use a single macro for all hyperlinks and have the computer figure out whether to use plain text or a hyperref, and if plain text, what plain text to put, without manually copying the \newlabels from the aux file for the text-and-problems file.
That appears to require the system, when creating the problems-and-solutions file, to first create an aux file for the text-and-problems file, and then to check, for each hyperlink, whether to get the content from that file and display it as plain text or to get it from the aux file for the problems-and-solutions file and display it as a hyperlink. Implementing that in LaTeX seems a little challenging. Is it doable? Or is there a better way?
\autorefrather than\ref, so I have been trying to understand how to follow your suggestion that "the definitions could be expanded to incorporate that." (I don't care about the page number, but I'd like the labels to capture the right name.) But I'm stuck --- can you give me a hint? – Martin J. Osborne Dec 21 '19 at 17:36\autoref. Compare the two code blocks to see what the difference is. :) – Werner Dec 21 '19 at 19:29\autorefand\ref, so I define\textautorefand\textref. 2. If, instead of\begin{theorem}, you say\begin{theorem}[Property \(\alpha\)], you get an error if\textfalse. But\begin{theorem}[Property $\alpha$]works fine. That seems to me to be a bug inhyperref. 3.\caption{The value of \(x\)}also causes a problem. I worked around that by using\caption[]{The value of \(x\)}. – Martin J. Osborne Dec 22 '19 at 17:19\textfalseand\solutionstrue("Not in outer par mode"). To address this issue, I have done two things. (1) After the\elsein the definition of thetextpartenvironment I have redefined thefigureenvironment using\renewenvironment{figure}[1]{}{}. (2) That redefinition causes an error with\caption. I have worked around that by using thecaptionpackage and putting\captionsetup{type=figure}at the start of each figure in the text; I cannot see any undesirable side effects. Is there a better way? – Martin J. Osborne Dec 22 '19 at 17:21