2

I need to get rid of mulitple instances of hyperlinked text ", foo bar" in my compiled latex document. It would be incredibly laborious to employ global find and replace on source code because each instance of the string has different code due to hyperlinking. Also, I would like to easily turn instances of the string off and on.

I have tried using lualatex based on an answer from @Mico at

https://tex.stackexchange.com/questions/213947/how-to-replace-text

Thus:


\documentclass[10pt]{article}


% !TEX TS-program = lualatex
\usepackage{luacode} 
\begin{luacode}


function myreplace1 ( s )
   s = s:gsub ( ", Foo Bar" , "" ) 
   return s 
end



\end{luacode}

\AtBeginDocument{\directlua{luatexbase.add_to_callback(%
   "process_input_buffer", myreplace1 , "myreplace1")}}


\usepackage{hyperref} 



\begin{document}

The Foo Bar at the end of this sentence disappears thanks to myreplace1, Foo Bar.

While this does not, \textbf{\hyperref[sec:Interesting]{Foo Bar}}. 

Though, oddly a version of myreplace1 that uses "Foo Bar" without the comma (s = s:gsub ( "Foo Bar" , "" )) works fine with \textbf{\hyperref[sec:Interesting]{Foo Bar}}.


\section{Interesting words} \label{sec:Interesting}

\end{document}

Output

The approach works using hyperlinked "foo bar", but not if the hyperlinked "foo bar" is preceeded with a comma and space ", foo bar", in which case the string is left unmolested in the compiled text. As far as I can tell, commas and spaces are not "magic" in lua. In any event using escape characters such as %, \\ and [[ ]] do not seem to have any effect on the final result.

Many thanks for your help!

5
  • 1
    Hmmm, I am unable to replicate the issue you say you encounter if I make your code compilable by adding \documentclass{article} immediately before \usepackage{luacode} and adding \begin{document}, Hello, foo bar World, and \end{document} on three separate lines at the end. I.e., LuaLaTeX outputs Hello World; put differently, the string , foo bar -- including the leading comma-whitespace substring -- is removed successfully from the input stream. Please augment your code snippet to produce an example in which the Lua function fails to operate as expected.
    – Mico
    Commented Jun 19, 2020 at 3:00
  • Thanks very much for looking at it, @Mico! I have edited the post and I hope it is more useful now.
    – Bob
    Commented Jun 19, 2020 at 5:43
  • Are you aware of the use of regular expressions to find and replace complex expressions ? I think it could easily solve the problem.
    – gigiair
    Commented Jun 19, 2020 at 6:00
  • Thanks for your comment @giglair. If I understand correctly, a regular expression find and replace will irretrievably alter the source code (though with much less effort than manually correcting every instance of the string). However, I would like the option of easily reintroducing the strings back again (without having to keep parallel versions of the tex files).
    – Bob
    Commented Jun 19, 2020 at 6:14
  • @bob Of course, regular expressions allow you to capture and restore strings. The use of regular expressions requires a little training depending on the editor you are using. With some editors it is possible to operate on the captures to modify them (Emacs for example)
    – gigiair
    Commented Jun 19, 2020 at 8:32

1 Answer 1

1

I used the following non-lualatex hack to solve my problem.


% Removes word and preceding space. If *, word and preceding space restored.
\NewDocumentCommand{\sil}{sm}{%
  \IfBooleanTF{#1}{#2}%
               {\unskip}%
}%

I surrounded undesired hyperlinks (and surrounding punctuations and spaces) with \sil{}(which stands for "silence"). For example, \sil{\textbf{\hyperref[sec:Interesting]{Foo Bar}}}. The hyperlinked text is now gone. A search and replace of \sil with \sil* easily restores the previously disappeared hyperlinks. It was a little bit of work to do all the pasting of the macro, but I like having the hyperlinks being readily restorable if required.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .