This question is a follow-up of Macro: Replace all occurrences of a word, but would like to ask for a possibility to do the same with words (e.g. foo) that are identical to the contents of certain or all \label{foo}s, and have those words automatically replaced into a \nameref{foo}s, unless of course for the one instance of \label{foo}.
Explanation of the MWE below:
The following contents of \labels{...} occur:
\label{The first section}\label{The foremost section}
That means that every other instance of text which is identical to any such content (other than the instances of the \labels{...} itself of course) should be automatically changed into a \nameref{...} with those contents.
For example, any other occurence in the text of:
- The first section
should be turned into (I am talking hyper-markup here, \nameref{ }should of course not be visible to the end-user):
\nameref{The first section}
which could then automatically become a clickable link to the
\label{The first section}
Analogue, this can be done for any number of labels (e.g. a Second section, a Third section, etc... could have analogue labels).
Note: it should not only work for (labels near) Sections, but it would be nice to have it functioning for any labels.
MWE
\documentclass{article}
\begin{document}
\section{The first and foremost section, with 2 nice labels called ``The first section", and ``The foremost section" respectively (without parentheses).}\label{The first section}\label{The foremost section}
This is some text, now let's imagine that you want to refer \textbf{automatically} to the section above, by simply typing the following words: The first section.\\
Alternatively, the string ``The foremost section" (without parentheses) should also automatically be changed into a \textbf{nameref} which automatically links to the label accordingly.
\end{document}
sedorawk) would be far more suitable. And any decent editor (even if there's really only one decent editor) could easily solve this problem. – jon Apr 20 '16 at 01:59sedorawk, or should we not further go in to that, as that is off-topic for the Tex community here? – O0123 Apr 20 '16 at 02:01footo\...{foo}and then\src{\src{foo}}to\src{foo}also seems needlessly complicated. Further more, one could tap in to the centralized lay-out formatting of\nameref{...}s, just to mention one example of how centralizing these things can make things go quicker, and sometimes more accurate (imagine you add an extra instance offoo-> then correcting for it becomes extra work). – O0123 Apr 20 '16 at 03:04\src{\src{foo}}unless you used a careless search-and-replace method. I don't understand why you're not going straight fromfooto\src{foo}.... Can't you simply search forfoo(i.e., with a leading space) and replace it with\src{foo}? TeX is not meant to be a swiss-army knife that you use for every type of problem (that's Perl!), so I can't endorse a non-TeX answer (even though they are possible). – jon Apr 20 '16 at 03:53[space]foo. There might very well be instances offoo(i.e. without leading spaces). – O0123 Apr 20 '16 at 03:58foowill appear and what (I guess) it will be embedded in to write a properregexp(for example, one step in the process could be to regularize howfooappears, if it's just going to be stuck in randomly as you are typing; then then next step would be to do a replacement -- not a difficult script in most cases). – jon Apr 20 '16 at 04:26sed, which is what I'd use.) – jon Apr 23 '16 at 02:11