TransWikia.com

How can I draw in an existing tikzpicture from an lstnewenvironment?

TeX - LaTeX Asked by barbaz on July 7, 2021

The below example works fine if I move the tikzpicture environment into the after command of the lstnewenvironment. However, if I want to draw in an existing tikzpicture, the node content is not shown. It grows vertically if I add more lines of code, but they aren’t visible:

documentclass{beamer}

usepackage{listings}
usepackage{tikz}

makeatletter
lstnewenvironment{code}{%
    lstset{%
        basicstyle=ttfamilyfootnotesize,
    }%
    setbox@tempboxa=hboxbgroupcolor@setgroup
}%
{%
    color@endgroupegroup
        node [fill=green]
            {box@tempboxa};
}

begin{document}

begin{frame}[fragile]

begin{tikzpicture}
    begin{code}
Some Code
    end{code}
end{tikzpicture}

end{frame}

end{document}

How can I fix this so that the box is shown properly but so that I can create multiple code boxes inside the same tikzpicture?

One Answer

You don't need the temporary box (and you were lucky node didn't overwrite it before executing box@tempboxa). You can let the environment body directly fill the node contents using node[fill=green] bgroup in the environment start code and egroup in its end code. Because it is node and not TeX's hbox primitive, I think the color@setgroup and color@endgroup are not necessary then, although they would probably not harm. One last thing: don't forget the makeatother!

documentclass{beamer}
usepackage{listings}
usepackage{tikz}

makeatletter
lstnewenvironment{code}{%
    lstset{basicstyle=ttfamilyfootnotesize}%
    node[fill=green] bgroup
}%
{%
    egroup ;%
}
makeatother

begin{document}

begin{frame}[fragile]

begin{tikzpicture}
    begin{code}
Some Code ($%@}{#~&!)
    end{code}
end{tikzpicture}

end{frame}

end{document}

enter image description here

Correct answer by frougon on July 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP