# Is this a good way to change \section in document using luacode? Or could this be done using plain Latex macro?

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{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.

• Looks more like a bug in hyperref. Here is a true MWE: dpaste.com/1YXE6G2 (expires in 7 days). – Henri Menke May 13 at 21:56
• Seems to be this one github.com/latex3/hyperref/issues/63 and this one github.com/wspr/unicode-math/issues/532 – Henri Menke May 13 at 22:01
• Does this answer your question? Strange error when unicode-math and hyperref are combined in xelatex – Henri Menke May 13 at 22:02
• @HenriMenke the true MWE is not really what I have. This problem shows up with I use \usepackage{unicode-math} \defaultfontfeatures{Scale=MatchLowercase} \setmathfont{Asana Math} \usepackage{Baskervaldx} And I do not want to change this as I like this font. But again, my question here is not on the error itself, but asking if this method is OK I used and if there is a macro in Latex to do this change instead. That is all. – Nasser May 13 at 22:12
• If you are sure that you will never use the optional argument of \section, then you can just write \cMakeRobust\section in the preamble. – Henri Menke May 13 at 22:46

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:
 \usepackage[bookmarks=false]{hyperref}
`