# Macro: Replace all occurrences of words which are identical to content of \label{…} into \nameref{…} (unless of course the \label itself)

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}

• Again, please provide an example making your intended usage clear and taking account of the restrictions you discussed with Mico in your other question. – cfr Apr 19 '16 at 22:38
• Please provide a bit more detail -- preferably in the form (possibly nonworking) compilable code -- that illustrates what exactly you're looking to achieve. – Mico Apr 19 '16 at 23:03
• @Mico + @ cfr -> The MWE (cf. above) just arrived from being shipped out in a compliable and compilable way. – Vincent Mia Edie Verheyen Apr 19 '16 at 23:15
• This would be best done outside TeX. Find & replace in the editor is one option, but a simple script could handle this for all cases automatically. – cfr Apr 20 '16 at 0:43
• I am sorry, but I'm not inclined to answer the general question when you already said it won't do what you need and that you anticipate asking for further modifications. Reliably good answers depend on good questions; and sometimes the questioner needs to do some of the hard work too. (And of course there are many free online resources for using regexp and sed, which is what I'd use.) – jon Apr 23 '16 at 2:11