2

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}
O0123
  • 1,773
  • 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. – O0123 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 00:43
  • You can't ping 2 people at once. The system will have told you that when you posted the comment. – cfr Apr 20 '16 at 00:44
  • This is not ideally done from within *TeX. Any number of text processing languages-focused languages (e.g., sed or awk) 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:59
  • @jon What do you mean with "there's really only one decent editor": which one then? Is it easily shown how to solve the question with sed or awk, 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:01
  • I was joking (... but it's emacs). It's a little off-topic as an answer though. But can't your editor do search-and-replace? Even word processors manage (almost) that much. – jon Apr 20 '16 at 02:46
  • @jon But it is as User nwp said in the question linked in the current question: Repeatedly search-and-replacing foo to \...{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 of foo -> then correcting for it becomes extra work). – O0123 Apr 20 '16 at 03:04
  • 1
    I cannot imagine why you'd ever end up with \src{\src{foo}} unless you used a careless search-and-replace method. I don't understand why you're not going straight from foo to \src{foo}.... Can't you simply search for foo (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
  • @jon Not every instance in the text will always have a leading space (represented here as [space]), as such: [space]foo. There might very well be instances of foo (i.e. without leading spaces). – O0123 Apr 20 '16 at 03:58
  • 1
    Hmm, well, it's impossible to give advice on how to write regular expressions when all the conditions aren't clearly laid out. You need to think about how foo will appear and what (I guess) it will be embedded in to write a proper regexp (for example, one step in the process could be to regularize how foo appears, 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:26
  • @jon First, we could try a general answer, which would fit expected regular text cases? – O0123 Apr 22 '16 at 21:07
  • 1
    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 02:11

0 Answers0