2

How can I find characters that are not curly braces? If I use this pattern "[^}]" in string.gsub I get an error.

EDIT: This is the part of the code that doesn't work:

\usepackage{luacode}

\begin{luacode}
    function colorize ( s ) 
        s = string.gsub ( s , "K_%w" , "\\textcolor[HTML]{00804d}{%0}" )
        s = string.gsub ( s , "(A)([^}])" , "\\textcolor[HTML]{005ce6}{%1}%2"))         
        return s
    end
\end{luacode}

\newcommand{\colorizeOn}{\directlua{luatexbase.add_to_callback("process_input_buffer", colorize , "colorize" )}}
\newcommand{\colorizeOff}{\directlua{luatexbase.remove_from_callback("process_input_buffer", "colorize" )}}

The first part of the script is ok (it substitutes all "K_" followed by an alphanumeric characters with the same thing but with a color code) while adding the second part (that should replace all "A" that are not followed by a "}" with "A" with its color code) it returns this errors:

Argument of \luacode@grab@lines has an extra }. s = string.gsub ( s , "(A)([^}
Paragraph ended before \luacode@grab@lines was complete. s = string.gsub ( s , "(A)([^}
Extra }, or forgotten \endgroup. s = string.gsub ( s , "(A)([^}
Missing \begin{document}. s = string.gsub ( s , "(A)([^}]
Missing \begin{document}. s = string.gsub ( s , "(A)([^}])" , "\\t

The main problem is the hat character in fact if I use this pattern "[}]" I get similar errors (the compiler expects to find a corresponding "{" before) and in my case I can easily fix that using this pattern instead "[{}]". The problem is that I want the negation of this pattern that should be written as "[^{}]" that doesn't work.

EDIT 2: this is what I expect.

  • 1
    Could you please add a complete example document of what your have tried and the exact error message you get? – siracusa Jul 14 '19 at 13:37
  • 1
    Observe that \textcolor[HTML]{005ce6}{A}BA} (from the external file you provided a link to) features an unbalanced curly brace and thus cannot be handled properly by LaTeX. – Mico Jul 14 '19 at 16:15
  • It was just an example, in my case I want to find all "A" that ar not bold \textbf{A} so I check if they are not followed by a "}" – PrinceOfBorgo Jul 14 '19 at 16:33
3

I suggest you use a luacode* environment instead of a luacode environment.

enter image description here

\documentclass{article}
\usepackage{xcolor,luacode}
\begin{luacode*}
function colorize ( s ) 
    s = string.gsub ( s , "K_%w" , "\\textcolor{red}{%0}" )
    s = string.gsub ( s , "(A)([^{}])" , "\\textcolor{blue}{%1}%2")     
    return s
end
\end{luacode*}

\newcommand{\colorizeOn}{\directlua{ luatexbase.add_to_callback( 
   "process_input_buffer", colorize , "colorize" )}}
\newcommand{\colorizeOff}{\directlua{ luatexbase.remove_from_callback( 
   "process_input_buffer", "colorize" )}}

\begin{document}
$K_b$, Au, A{u}

\colorizeOn
$K_b$, Au, A{u}
\end{document}
| improve this answer | |
  • Thanks but I have another problem, if I write something like this \textbf{A}$\,\to\,$\textbf{B} inside a \colorizeOn - \colorizeOff environment (where even for B I use the same lua substitution rule as A) I get this strange output A005ce6AMSsymbols[HTML]005ce6A]005ce6AMSsymbols[HTMLB. If I write the same line \textbf{A}$\,\to\,$\textbf{B} twice or more times only the first occurence is formatted as above while the others are formatted correctly (not modified since A and B are followed by a curly brace). – PrinceOfBorgo Jul 14 '19 at 20:09
  • The lua rule I used for substitution is s = string.gsub ( s , "(A)([^{}])" , "\\textcolor[HTML]{005ce6}{%1}%2" ) and s = string.gsub ( s , "(B)([^{}])" , "\\textcolor[HTML]{005ce6}{%1}%2" ) – PrinceOfBorgo Jul 14 '19 at 20:15
  • Sorry, the strange output is A005ce6AMSsymbols[HTML]005ce6A]005ce6AMSsymbols[HTML]005ce6B] → B – PrinceOfBorgo Jul 14 '19 at 20:29
  • @PrinceOfBorgo - I'm afraid I am unable to replicate the issue(s) you report. Incidentally, one should never write \textbf{A}$\,\to\,$\textbf{B}; it should be $\mathbf{A}\to\mathbf{B}$. (Note that the whole expression is a single "formula". Also, \, ("thinspace") isn't the correct amount of whitespace between math atoms of type math-ord and math-rel, respectively.) By the way, just employ a single gsub rule: s = string.gsub ( s , "([AB])([^{}])" , "\\textcolor[HTML]{005ce6}{%1}%2"). – Mico Jul 15 '19 at 4:19

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.