5

In this M(n)WE, there in a loss of a semicolon.

The tex.print part gives the good character chain it used directly in the new command, but as I want to use it in an external \draw, tikz claims for a semicolon that at put where it should be.

\documentclass{article}
\usepackage{fontspec}

\usepackage{tikz,xparse} 
\usetikzlibrary{quotes,through,calc,intersections,backgrounds}
\usepackage{luacode}
\usepackage[margin=1cm]{geometry}

\luadirect{require "LTG_Droites"}

\NewDocumentCommand{\Dt}{%
    O{1.2,1.2}  % extention
    m           % nom de la droite
    }{%
    \directlua{Draw_Droite(\luastring{#2},#1)}
    % the following works in tikzpicture
    % but want just the coordinate part
    %\draw \directlua{Draw_Droite(\luastring{#2},#1)} ;
}

\begin{document}

\begin{tikzpicture}
% doesn't work
%\draw \Dt{d1} ;
\end{tikzpicture}

 \Dt{d1} ;

\end{document}

And the LTG_Droites.lua

-- #### Draw_Droite
-- #### 
-- #### Renvoie (A)--(B) + extention

function Draw_Droite ( d , extA , extB )
    local A = "(0,0)"
    local B = "(1,1)"
    tex.print("($"..A.."!"..extA.."!"..B.."$)--($"..B.."!"..extB.."!"..A.."$)")
end
6

You need the code after \draw to expand to tikz syntax:

\RequirePackage{luatex85}
\documentclass{article}
\usepackage{fontspec}

\usepackage{tikz,xparse} 
\usetikzlibrary{quotes,through,calc,intersections,backgrounds}
\usepackage{luacode}
\usepackage[margin=1cm]{geometry}

\luadirect{require "LTG_Droites"}

\DeclareExpandableDocumentCommand{\xDt}{%
    O{1.2,1.2}  % extention
    m           % nom de la droite
    }{%
    \directlua{Draw_Droite(\luastring{#2},#1)}%
    % the following works in tikzpicture
    % but want just the coordinate part
    %\draw \directlua{Draw_Droite(\luastring{#2},#1)} ;
}

%force full expansion of \Dt in two  expansion steps.
\newcommand\Dt{\romannumeral`\^^@\xDt}

\begin{document}

\begin{tikzpicture}
% doesn't work
\draw \Dt{d1} ;
\end{tikzpicture}

\Dt{d1} ;

\end{document}
| improve this answer | |
  • What is \RequirePackage{luatex85} for ? I don't understand. – Tarass May 22 '16 at 14:49
  • @Tarass luatex0.85+ (in particular luatex 0.95 in texlive 2016 which I'm using) is massively incompatible with previous luatex and with pdftex, luatex85 package (which you should have already in an up to date texlive or miktex) emulates many of the commands that have been removed (so pgf still works) – David Carlisle May 22 '16 at 16:16
  • I never used such packages. Your answer works without. What does it bring in this cases? – Tarass May 22 '16 at 16:19
  • @Tarass you presumably have an older version of luatex. If you update luatex (before the pgf maintainers update pgf) all your documents using tikz code will stop working unless you load that compatibility package. in older luatex (such as 0.80 in texlive 2015 the package does nothing) – David Carlisle May 22 '16 at 16:22
  • 1
    @AlainMatthes I have updated the answer to work with current tikz/expl3 – David Carlisle Jan 12 '18 at 12:24

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.