JuliaTeX / TikzPictures.jl

Creating PGF/TikZ pictures and saving them in various formats
Other
89 stars 28 forks source link

Struggling with preamble #49

Closed tamasgal closed 5 years ago

tamasgal commented 5 years ago

I am trying to define my own node style in a separate \tikzset and as far as I understood the only way to pass them to TikzPictures is to use the preamble= argument.

This is what I am trying to set:

\documentclass{article}
\usepackage{tikz}

\tikzset{
  pics/mynode/.style args={#1,#2,#3}{
     code={
       \draw (0,0) -- (1,-1) -- (2,0) -- (2,2) -- (0,2) -- (0,0);
       \node[#3] (#1) at (1,1) {#2};
     }
  }
}

\begin{document}

  \begin{tikzpicture}
      \draw (0,0) pic{mynode={A, Hi, blue}};
      \draw (0,3) pic{mynode={B, Hello, red}};
      \draw (2,1.5) pic{mynode={C, Bye,}};
      \draw[thick, blue] (A)--(B)--(C)--(A);
  \end{tikzpicture}

\end{document}

I set the preamble using:

preamble = L"""
\tikzset{
  pics/mynode/.style args={#1,#2,#3}{
     code={
       \draw (0,0) -- (1,-1) -- (2,0) -- (2,2) -- (0,2) -- (0,0);
       \node[#3] (#1) at (1,1) {#2};
     }
  }
}
"""

and then tried

using TikzPictures

tp = TikzPicture(L"""
[all exes=unit length=20cm]
\draw (0,0) -- (1,1);
\draw (1,0) -- (0,1);
\node at (0.5,0.5) {tikz $\sqrt{\pi}$};
"""
, preamble=preamble)

However, I got

TikzPicture(L"[all exes=unit length=20cm]
\draw (0,0) -- (1,1);
\draw (1,0) -- (0,1);
\node at (0.5,0.5) {tikz $\sqrt{\pi}$};
", "scale=1", L"$\tikzset{
  pics/mynode/.style args={#1,#2,#3}{
     code={
       \draw (0,0) -- (1,-1) -- (2,0) -- (2,2) -- (0,2) -- (0,0);
       \node[#3] (#1) at (1,1) {#2};
     }
  }
}
$", true)
! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.2 $
   \tikzset{

So wondering how the preamble is to be used? I played around but could not figure out.

Also I think it would be nice to provide the path to a preamble file using a environment variable, just like in PGFPlotsX.jl. I'll try to follow up with an MR.

tamasgal commented 5 years ago

Oops, it turned out to be the L"..." string type, which inserts $ at the beginning and the end. Now I switched to raw"..."