3

I am trying to use a combination of Latex and sage to create tasks for my pupils. Therefore I have to replace two different types of expressions

2*x shall become 2 x

and

x*3 shall become x\cdot 3

\documentclass[utf8,fleqn]{scrartcl}

\usepackage{l3regex}


\begin{document}
\ExplSyntaxOn

\tl_set:Nn \l_my_tl { 2*x-x*4 }
\regex_replace_all:nnN { [a-z]\*\d+ } { \1 \c{cdot} \2 } \l_my_tl

$\l_my_tl$

\end{document}

This gives me 2*x-\cdot instead of 2*x-x\cdot 4

My question is, how can I refer to the separate matches found by l3regex?

  • 2
    You shouldn't pile up new questions. Please, make a new one and roll back this one to the original. – egreg Feb 7 at 11:21
4

You need to capture the matches: this is standard for any regex, not just for the expl3 engine

\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\tl_set:Nn \l_my_tl { 2*x-x*4 }
\regex_replace_all:nnN { ([a-z])\*(\d+) } { \1 \c{cdot} \2 } \l_my_tl
$\l_my_tl$
\end{document}
| improve this answer | |
  • I've not explicitly loaded l3regex as it was made part of the expl3 core some time ago – Joseph Wright Feb 7 at 9:44
2

You can transform “letter *” into “letter \cdot” and then get rid of the remaining asterisks.

\documentclass{article}

\usepackage{expl3}

\begin{document}
\ExplSyntaxOn

\tl_set:Nn \l_tmpa_tl { 2*x-x*4 }
\regex_replace_all:nnN { ([a-z])\*(\d+) } { \1 \c{cdot} \2 } \l_tmpa_tl
\regex_replace_all:nnN { \* } { } \l_tmpa_tl

$\l_tmpa_tl$

\end{document}
| 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.