3

I try to pass characters to lua but I don't want to typeset the "" that are not natural in latex syntax. Here is a dummy mwe.

Here is the background :

\documentclass{article}
\usepackage{luatex,tikz,siunitx}

\directlua{dofile("DteGraduee.lua")}

\tikzset{%
    % styles des étiquettes de graduation
    bottick/.style={font=\footnotesize,anchor=north},
    toptick/.style={font=\footnotesize,anchor=south},
    }

\makeatletter

\pgfkeys{/DteG/.cd,
        % longueur de l'axes sans le nez
        longueur/.store in=\@long,
        longueur=7,
        % minimum en x
        Xmin/.store in=\@min,
        Xmin=0,
        % maximum en X
        Xmax/.store in=\@max,
        Xmax=10,
        % le premier nombre est la graduation principale
        % le deuxième est la division de la graduation principale
        % le troisème est la division de la sous graduation
        % pour du papier millimétré : {1,2,5}
        % on les unités, les demis et les dixièmes.
        pas/.store in=\@pas,
        pas={1,10},
        % nombre de niveau de graduation à étiqueter
        nb axes grad/.store in=\@AxeG,
        nb axes grad=1,
        % longueur du nez de la flèche en plus de la "longueur"
        nez/.store in=\@nez,
        nez=.333,
        % longueurs de marques de graduation
        mark length/.store in=\@mkl,
        mark length=.150,
        sub mark length/.store in=\@smkl,
        sub mark length=.120,
        sub sub mark length/.store in=\@ssmkl,
        sub sub mark length=.075,
        % épaisseur des marques de graduations
        mark/.style={semithick},
        submark/.style={line width=.4pt},
        subsubmark/.style={thin},
        % style de l'axe
        axe/.style={semithick,-latex},
        droite/.code={%
            \directlua{%
            GradLength = {\@mkl, \@smkl, \@ssmkl}
            GradStyle = {"mark", "submark", "subsubmark"}
            Dte_Grad (\@long, \@min, \@max, {\@pas}, \@AxeG, \@nez)
            }
        }
}

\makeatother

\begin{document}

\begin{tikzpicture}[baseline, label distance = -.1cm]

    \path[/DteG/.cd, Xmin=7.78, Xmax=7.93 ,pas={.1,10}, droite] ;
    \node[label=above:$M$] at (B3) {} ;
    \node[label=above:$N$] at (B5) {} ;
    \node[label=above:$P$] at (B11) {} ;
    \node[label=above:$R$] at (B13) {} ;

    \end{tikzpicture}

\end{document}

The DteGraduee.lua file :

tp = tex.print

function Dte_Grad (longueur, Xmin, Xmax, pas, AxeGrad, nez)

local step = {}

local GradTickT = {"A", "B", "C", "D"}
local GradTickB = {"a", "b", "c", "d"}


    function x (X)
        return (X - Xmin)*longueur/(Xmax - Xmin)
    end

    -- dessin de l'axe
        tp("\\draw[/DteG/axe] (0,0)--(".. longueur + nez ..",0) ;")

    -- node phantom qui permet l'alignement à gauche
        tp("\\node[anchor=north,font=\\footnotesize,opacity=0] {\\num{0}} ;") 

    j = 1
    step[1] = 0
    step[2] = pas[1]
    repeat

        if j > 1
        then
            step[j+1] = step[j] / pas[j]
        end

        -- calcul de l'abscisse du premier point de la graduation
        if Xmin % step[j+1] == 0
        then
            start = Xmin
        else
            start = Xmin + (step[j+1] - Xmin % step[j+1])
        end

        k = 0 -- rang des ticks

        -- boucle de graduation
        repeat
            -- convertit l'abscisse courante en abcisse sur le dessin
            i = x(start)

            -- on ne dessine pas la première marque sur l'axe
            -- si ce n'est pas zéro
            if start == 0 or math.abs(i)>.001
            then
                -- dessine le trait
                tp("\\draw[/DteG/"..GradStyle[j].."] ("
                    ..i..","..GradLength[j]..
                    ") coordinate ("..GradTickT[j]..k..")--("
                    ..i..",-"..GradLength[j]..
                    ") coordinate ("..GradTickB[j]..k..")")

                -- étiquettes éventuelles sur la graduation
                if AxeGrad >= j and start % step[j] ~= 0
                then
                    tp("node[anchor=north,font=\\footnotesize] {\\num{"..start.."}}") 
                end

                -- on ferme le draw
                tp(";" )
            end
            -- on incrémente
            start = start + step[j+1]
            k = k + 1

        -- abscisse suivante ou sortie
        until start > Xmax+.00001       -- arrondi supérieur

    -- sous graduation suivant ou sortie
    j = j + 1
    until pas[j] == nil

end

The result :

enter image description here

Here is what I am able to do for the moment. I want to pass the name of the points and their coordinate using the pgf keywords synthax. To avoid multiple "", one can do :

\documentclass{article}
\usepackage{tikz,luatex}

\directlua{dofile("luatest.lua")}

\pgfkeys{tikz/.cd,
        points/.store in=\points,
        points="",
    }

\begin{document}
\begin{tikzpicture}[points={"A,1,B,2"}]

    \directlua{%
    bob = \points
    list ()
    }

\end{tikzpicture}
\end{document}

and luatest.lua

function list ()
Point = {}

j = 1
for word in string.gmatch(bob, '([^,]+)') do
    Point[j] = word
    j = j + 1
end

j = 1
    while Point[j] ~= nil
    do
    tex.print("\\node at ("..Point[j+1]..",0) {"..Point[j].."} ;")
        j = j + 2
    end
end

With help of this question Lua need to split at comma

  • I don't have an environment with which to test, but my guess would be to simply remove them from your input: \begin{tikzpicture}[points={"A",1,"B",2}] to \begin{tikzpicture}[points={A,1,B,2}]. I've never worked with LuaTeX, though, so I may be way off. – Sean Allred Oct 6 '14 at 19:43
  • 1
    The title and the second paragraph of your posting seem to be at cross-purposes. Is your concern about the use of double quotes on the Lua side or on the TeX side of things? – Mico Oct 6 '14 at 19:46
  • @mico Lua need "A" otherwise it considers Point[1] as nil because A is nil. But don't want to typeset "A" in latex, so is there a way to add the "" automaticaly, or another way to pass characters ? Is it more clear ? – Tarass Oct 6 '14 at 19:53
  • Use TeX instead of Lua and it will work or do you have in real a more complex structure? – user2478 Oct 7 '14 at 9:16
  • @Herbert Yes I have a complex use for that. I can post it, not very long but not minimal ;-) – Tarass Oct 7 '14 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.