1

I have been playing with pylatex lately, I tried to define a global style so that all pictures of my document can use this style. in this example an object named 'my_element' has a specific style. for example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.multipart,chains,arrows}

    \begin{document}
        
        \begin{tikzpicture}[my_elem/.style={rectangle split, rectangle split parts=2,
                draw, rectangle split horizontal}, >=stealth, start chain]
            
            \node[my_elem,on chain] (A) {12};
    
        \end{tikzpicture}
        
    \end{document}

but It kept failing, when I tried using the pylatex :

from pylatex import (Document, TikZ, TikZNode,
               Command, Package,
               TikZDraw, TikZCoordinate,
               TikZUserPath, TikZOptions, UnsafeCommand)

# create document
doc = Document()

doc.preamble.append(Command('usetikzlibrary', 'calc,shapes.multipart,chains,arrows'))

# Here I'm having some difficulties defining the style for my_elem
mdf_style_definition = UnsafeCommand('\\my_elem',
                                     arguments={'style': ['rectangle split']})


with doc.create(TikZ(options=TikZOptions(mdf_style_definition.dumps_as_content()))) as pic:

    node_chain = TikZNode(text=str(12),
                          handle='A',
                          options=TikZOptions('my_elem',
                                              'on chain'))

    pic.append(node_chain)

doc.generate_pdf('my_solution', clean_tex=False)

Any suggestion how to define this style with pylatex?

2
  • Can you make a complete Python-example, so that we can run the code without modifications to get a generated .tex-file? Commented May 19, 2021 at 2:37
  • Sure, I have added the complete Python code. I'm trying to reach to the same tex file content, which I wrote above. Commented May 19, 2021 at 6:46

1 Answer 1

1

If you look at generated .tex-file, you'll see that you get

\begin{tikzpicture}[\\my_elem{style=['rectangle split']}]%

which is obviously wrong. And makes me realize that using UnsafeCommand is probably wrong. A TikZ style is not a macro, the syntax is entirely different.

Looking a bit at the PyLaTeX site, there doesn't seem to be a class to define TikZ styles though. Something simple that works is to just write the style definition directly in TikZOptions:

with doc.create(TikZ(options=TikZOptions('my_elem/.style={rectangle split}','start chain'))) as pic:

I suppose if you want a TikZstyle class in pylatex you'll have to define it yourself, or post a feature request at https://github.com/JelteF/PyLaTeX and hope the developer implements it.

Complete Python code and the .tex file it generates.

from pylatex import (Document, TikZ, TikZNode,
               Command, Package,
               TikZDraw, TikZCoordinate,
               TikZUserPath, TikZOptions, UnsafeCommand)

# create document
doc = Document()

doc.preamble.append(Command('usetikzlibrary', 'calc,shapes.multipart,chains,arrows'))



with doc.create(TikZ(options=TikZOptions('my_elem/.style={rectangle split}','start chain'))) as pic:

    node_chain = TikZNode(text=str(12),
                          handle='A',
                          options=TikZOptions('my_elem',
                                              'on chain'))

    pic.append(node_chain)

doc.generate_tex('my_solution')
\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{tikz}%
%
\usetikzlibrary{calc,shapes.multipart,chains,arrows}%
%
\begin{document}%
\normalsize%
\begin{tikzpicture}[my_elem/.style={rectangle split},start chain]%
\node[my_elem,on chain] (A) {12};%
\end{tikzpicture}%
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .