Quantcast
Channel: Active questions tagged mathtools - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 122

How can I insert math expressions which aren't simple strings into my lua-tikz setup?

$
0
0

I am building a package, part of which should be able to simply append mathematics to a picture. Unfortunately, it currently only works for simple strings.

How could I insert the following math expression into a tikzpicture using my approach?

\documentclass[    tikz    ,border = 1cm]{standalone}\usepackage{tikz,nicematrix,mathtools,unicode-math}\makeatletter\tikzset{    /lua-tikz3dtools/.is family % abbreviated "td"    ,/lua-tikz3dtools/.cd         ,/lua-tikz3dtools/parametric/.cd % appreviated "p"            ,/lua-tikz3dtools/parametric/label/.cd % abbreviated "l"                ,x/.code = {\edef\luatikztdtools@p@l@x{#1}}                ,y/.code = {\edef\luatikztdtools@p@l@y{#1}}                ,z/.code = {\edef\luatikztdtools@p@l@z{#1}}                ,name/.code = {\edef\luatikztdtools@p@l@name{#1}}                ,transformation/.code = {\edef\luatikztdtools@p@l@transformation{#1}}                ,x = {0}                ,y = {0}                ,z = {0}                ,name = {George}                ,transformation = {identity_matrix()}}\ExplSyntaxOn\lua_load_module:n { parametric }\NewDocumentCommand { \appendlabel } {o} {    \group_begin:    \tikzset{        /lua-tikz3dtools/parametric/label/.search~also = {/tikz}        ,/lua-tikz3dtools/parametric/label/.cd        ,#1    }    \__lua_tikztdtools_appendlabel:    \group_end:}\NewDocumentCommand { \displaysegments } {} {    \group_begin:    \__lua_tikztdtools_displaysegments:    \group_end:}\ExplSyntaxOff\makeatother\begin{document}    \begin{tikzpicture}        \appendlabel[            x = 1            ,y = 1            ,name = {\(                \underbrace{                \begin{bNiceArray}[margin]{r|r}                    1 & 2 \\                    3 & 4                \end{bNiceArray}                =                \begin{bNiceArray}[margin]{r|r}                    1 & 2 \\                    3 & 4                \end{bNiceArray}                }            \)}        ]        \displaysegments    \end{tikzpicture}\end{document}
-- https://tex.stackexchange.com/a/747040--- Creates a TeX command that evaluates a Lua function------ @param name string The name of the `\csname` to define--- @param func function--- @param args table<string> The TeX types of the function arguments--- @param protected boolean|nil Define the command as `\protected`--- @return nillocal function register_tex_cmd(name, func, args, protected)    -- The extended version of this function uses `N` and `w` where appropriate,    -- but only using `n` is good enough for exposition purposes.    name = "__lua_tikztdtools_" .. name .. ":" .. ("n"):rep(#args)    -- Push the appropriate scanner functions onto the scanning stack.    local scanners = {}    for _, arg in ipairs(args) do        scanners[#scanners+1] = token['scan_' .. arg]    end    -- An intermediate function that properly "scans" for its arguments    -- in the TeX side.    local scanning_func = function()        local values = {}        for _, scanner in ipairs(scanners) do            values[#values+1] = scanner()        end        func(table.unpack(values))    end    local index = luatexbase.new_luafunction(name)    lua.get_functions_table()[index] = scanning_func    if protected then        token.set_lua(name, index, "protected")    else        token.set_lua(name, index)    endendlocal segments = {}local function single_string_expression(str)    return load(("return %s"):format(str), "expression", "t", _G)()endlocal function append_label(hash)    local x              = hash.x    local y              = hash.y    local name           = hash.name    x = single_string_expression(x)    y = single_string_expression(y)    table.insert(        segments,         {             segment = {{x, y, 0, 1}},             type = "point",            name = name        }    )endregister_tex_cmd("appendlabel",    function()        append_label{            x              = token.get_macro("luatikztdtools@p@l@x"),            y              = token.get_macro("luatikztdtools@p@l@y"),            name           = token.get_macro("luatikztdtools@p@l@name")        }     end,    { })local function display_segments()    -- Iterate through the sorted segments for rendering    for _, segment in ipairs(segments) do        if segment.type == "point" and segment.name then            local P = segment.segment[1]            tex.sprint(string.format("\\node at (%f,%f) {%s};",                P[1], P[2]                ,segment.name            ))        end    end    -- Clear the segments array after processing    segments = {}endregister_tex_cmd("displaysegments", function() display_segments() end, { })

Viewing all articles
Browse latest Browse all 122

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>