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?

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

2 Answers 2

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}
1
  • I've not explicitly loaded l3regex as it was made part of the expl3 core some time ago
    – Joseph Wright
    Commented Feb 7, 2020 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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .