# Expl3 regex and missing semicolon?

Try to make an answer to this question How to go about creating custom parser in Latex, I have the following problem. The result of the macro seem no conform to what I expect :

(1:1)--(-1:2)--(2,2)


or there is a problem related to expansion ...

\documentclass{article}
\usepackage{tikz,expl3,xparse}

\ExplSyntaxOn

\NewDocumentCommand{\makelinesegment}{ m } {
\tl_set:Nn \l_bob_func_tl { #1 }
\regex_replace_all:nnN { ([0-9])- } { \1)--( } \l_bob_func_tl
\regex_replace_all:nnN { : } { , } \l_bob_func_tl
(\l_bob_func_tl)
}

\ExplSyntaxOff

\begin{document}

\makelinesegment{1:1--1:2-2:2}

% If uncommented
% Package tikz Error: Giving up on this path. Did you forget a semicolon?.
%\tikz\draw \makelinesegment{1:1--1:2-2:2} ;

\end{document}

• It's not expandable. Mar 22 '18 at 17:57
• You either do \makelinesegment{1:1--1:2-2:2}\tikz\draw\l_bob_func_tl; or program \pgfextra in your command if you are going to use it inside TikZ. Mar 22 '18 at 17:59
• \draw expects to see something like (1,1)--(1,2)--(2,2), not the set of instructions necessary to get those tokens. Mar 22 '18 at 18:12
• @egreg More precisely, it expects to see something that gives those tokens within 100 expansions (that could be replaced by the \romannumeral trick ...) Mar 22 '18 at 18:16

If you are only going to use this inside TikZ:

\documentclass{article}

\usepackage{tikz,xparse}

\ExplSyntaxOn

\tl_new:N \l_bob_func_tl

\NewDocumentCommand \makelinesegment { m }
{
\pgfextra
\tl_set:Nn \l_bob_func_tl { #1 }
\regex_replace_all:nnN { ([0-9])- } { \1)--( } \l_bob_func_tl
\regex_replace_all:nnN { : } { , } \l_bob_func_tl
\exp_last_unbraced:NNV % this is not necessary, but I find it cleaner, correct me if I'm wrong
\endpgfextra
(\l_bob_func_tl)
}

\ExplSyntaxOff

\begin{document}

% \makelinesegment{1:1--1:2-2:2} % do not use outside \tikz or {tikzpicture}

\tikz\draw\makelinesegment{1:1--1:2-2:2};

\end{document}


If you need outside too we need to add a boolean somewhere, say it if you need it.

• It's fine without Boolean. Mar 22 '18 at 18:16
• I would personally use an expandable command to get it; using regex seems like an overkill (to mee using l3regex seems like driving a HUMMER to go to the grocery store down the corner). Mar 22 '18 at 18:21
• You broke my hart ;-) It was just a beginner exercice to discover expl3 wonderworld, as it seems hostile for now, I took my Hammer. Mar 22 '18 at 19:04
• What is the meaning of \exp_last_unbraced:NNV. Mar 22 '18 at 19:12
• @Tarass Don't worry, it's not the end of the (wonder)world. \exp_last_unbraced:NN takes three tokens, gets the value of the last one, what's inside the last (i.e., expands the last one once), and leaves the first two untouched. Example: \def\a{A} \def\b{B} \def\c{C}, then \exp_last_unbraced:NNV \a \b \c would expand to \a\b C and then expansion would continue normally (this contrasts with \exp_args:NNV \a \b \c that would leave \a\b{C}). In this particular case the three arguments are \endpgfextra, (, and \l_bob_func_tl. Mar 22 '18 at 19:47