3

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. – Manuel Mar 22 '18 at 17:57
  • 1
    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. – Manuel Mar 22 '18 at 17:59
  • 1
    \draw expects to see something like (1,1)--(1,2)--(2,2), not the set of instructions necessary to get those tokens. – egreg Mar 22 '18 at 18:12
  • 1
    @egreg More precisely, it expects to see something that gives those tokens within 100 expansions (that could be replaced by the \romannumeral trick ...) – Joseph Wright Mar 22 '18 at 18:16
4

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.

| improve this answer | |
  • It's fine without Boolean. – Tarass 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). – Manuel 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. – Tarass Mar 22 '18 at 19:04
  • What is the meaning of \exp_last_unbraced:NNV. – Tarass 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. – Manuel Mar 22 '18 at 19:47

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.