0

I wanted to do simple thing. Change each \section to \cprotect\section in the whole document.

I could not figure out how to do it using latex macro. So I used lua

I'd like to ask if you think this is an OK method or if there is a better way to do it using pure latex macro, which might be more efficient.

First will show MWE showing why I needed to do this.

If you are wondering why not use an editor and do this? It is because I can't edit the latex code itself after \begin{document} because I use an Latex editor (Scientific word) which does not understand the command \protect. So if I modify it using a text editor outside of SW, then SW will not be able to read/understand the document any more.

But I could only do this in preamble, since I can hide preamble from SW easily.


\documentclass[12pt]{article}

\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Asana Math}
\usepackage{Baskervaldx}

\usepackage{amsmath}
\usepackage{hyperref}

\begin{document}
\tableofcontents
\section{$\cos\left(  A+B\right)  $ and $\sin\left(  A+B\right)  $}%
    this is my section
\end{document}

The above gives an error when compiling using lualatex

Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref)                removing `math shift' on input line 13.

! Improper alphabetic constant.
<to be read again>
\math@bgroup
l.13 ...+B\right)  $ and $\sin\left(  A+B\right)  $}
                                                  %
?

This version below uses lua to modify each \section to \cprotect\section on the fly, by adding code to the preamble only without modify the body of the document, which is what I wanted


\documentclass[12pt]{article}    
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Asana Math}
\usepackage{Baskervaldx}    
\usepackage{amsmath}
\usepackage{hyperref}    
\usepackage{cprotect}
\usepackage{luacode,luatexbase} 

\AtBeginDocument{%
\luaexec{luatexbase.add_to_callback 
   ( "process_input_buffer", changeSection, "changeSection" )}%
}

\begin{luacode}
function changeSection(s)  
      --texio.write_nl("Enter with s="..s)
      s = string.gsub(s,"\\section" , "\\cprotect\\section")
   return (s)
end
\end{luacode}

\begin{document}
\tableofcontents
\section{$\cos\left(  A+B\right)  $ and $\sin\left(  A+B\right)  $}%
    this is my section
\end{document}

And now lualatex compiles it OK and output PDF is correct.

Question is: Could the above be done using direct Latex macro? I tried renewcommand and that does not work.

I am not unhappy with the above, but worry it might be inefficient since it has to do lots of string searches each time on whole document. I plan to do the same for subsection and subsubsection.

Please note that I only use lualatex.

TL 2020

Update

For the error above, I tried the "fix" in the suggested link, it still gives the error:

\documentclass[12pt]{article}    
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Asana Math}
\usepackage{Baskervaldx}

\usepackage{amsmath}
\usepackage[unicode,psdextra]{hyperref}[2012/08/13]

\begin{document}
\tableofcontents
\section{$\cos\left(  A+B\right)  $ and $\sin\left(  A+B\right)  $}%
    this is my section
\end{document}

Compile using lualatex

! Improper alphabetic constant.
<to be read again>
\math@bgroup
l.13 ...+B\right)  $ and $\sin\left(  A+B\right)  $}
                                                  %
?

My question here is not on the error above per say, but if there is a better way to change \section to \cprotect\section I only used this example which generates an error to try to justify why I wanted to do this in first place.

1

cprotect works as it hides the argument (it writes it to a file) and hyperref no longer can process it. But this also means that it no longer can appear in the outlines/bookmarks. Instead you only see a file name there:

enter image description here

So a better solution -- until a way is found to avoid that hyperref errors with such math -- is not to use bookmarks at all with

 \usepackage[bookmarks=false]{hyperref}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.