ho-tex / soul

LaTeX package "soul" for letterspacing, underlining, striking out and other evil things
LaTeX Project Public License v1.3c
5 stars 1 forks source link

Highlighting math in LaTeX-style math-mode delimiters \(…\) #6

Open ghost opened 1 year ago

ghost commented 1 year ago

Please integrate http://tex.stackexchange.com/a/443317 or http://tex.stackexchange.com/a/426036 (or an equivalent way of solving the issue) into soul.sty.

Test case failing as of now:

\documentclass{standalone}
\usepackage[x11names]{xcolor}
\usepackage{soul}
\begin{document}
Please, highlight \hl{\(e^{i\pi}=-1\) and \emph{The Pythagorean Theorem \(a^2+b^2=c^2\)}}.
\end{document}
michaelcadilhac commented 10 months ago

Seconded; it's not a limitation that is easy to Google or know about without reading the finer details of the documentation. It's also very unexpected, and there's a solution provided that works immediately.

u-fischer commented 10 months ago

Well the code with l3regex is too slow imho, but the etl-code from @Skillmon could be ok.

But there are clearly not enough test files in the repository to really check if they are unwanted side effects. So if you want that, help me to improve the test situation: give me three small documents which tests some special soul case.

michaelcadilhac commented 10 months ago

Haha, is that a trade? :-) What's the test format?

ghost commented 9 months ago

@u-fischer I often use LaTeX math \(…\), emphasis, and German quotation marks in German texts. Therefore, my test case is this:

\documentclass[ngerman]{article}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[x11names]{xcolor}
\usepackage{soul}
\begin{document}
Bitte \hl{die „schönste“ Gleichung \(e^{i\pi}=-1\) und \emph{den „Satz des Pythagoras“ \(a^2+b^2=c^2\)}} farbig hinterlegen.
% \hl{die schönste Gleichung \(e^{i\pi}=-1\)}%%% for the purposes of this very bug report, you might wish not to mix the issues and take, instead of the line above, only this line.
\end{document}
Skillmon commented 9 months ago

@u-fischer the code using \etl_token_replace_all_deep:nNn needs to step through the entire list twice (with all checks for head-is-space and head-is-group), I think the performance could further be improved by coding up some customary replacer with \etl_act:nnnnn that only needs to step through the list once.

I didn't benchmark this, but I reckon this is faster (it also doesn't use \etl_token_if_eq:NNTF, which is overkill because it requires string-comparison plus the \token_if_eq_meaning:NNTF -- something that's probably unnecessary for this application):

\documentclass{article}

\usepackage{soul}
\usepackage{etl}

\ExplSyntaxOn
\cs_set_eq:NN \SOUL_math_start: \(
\cs_set_eq:NN \SOUL_math_end:   \)
\cs_set_eq:Nc \SOUL_start:n { SOUL@start }
\cs_set_protected:cpn { SOUL@start } #1
  { \exp_args:Ne \SOUL_start:n { \__SOUL_math_fixer:n {#1} } }
\cs_new:Npn \__SOUL_math_fixer:n
  {
    \etl_act:nnnnn
      \__SOUL_math_fixer_aux:nN
      { \use_i:nn { ~ } }
      \__SOUL_math_fixer_aux:nn
      {}
  }
\cs_new:Npn \__SOUL_math_fixer_aux:nn #1#2
  { { \__SOUL_math_fixer:n {#2} } }
\cs_new:Npn \__SOUL_math_fixer_aux:nN #1#2
  {
    \token_if_eq_meaning:NNF #2 \SOUL_math_start:
      { \token_if_eq_meaning:NNF #2 \SOUL_math_end: \use_iii:nnn }
    $ \use_none:n { \exp_not:N #2 }
  }
\ExplSyntaxOff

\begin{document}
Bitte \hl{\emph{den Satz des Pythagoras \(a^2+b^2=c^2\)}} farbig hinterlegen.
\end{document}