9

I have just started learning some TeX programming and am learning TikZ in plain TeX. My question is why is the following not working as intended; I try to define \sty as the TikZ drawing parameter, but it does not seem to be working.

\nopagenumbers
\input tikz.tex

\def\center#1\endcenter{\centerline{#1}}

\def\sty{step=.5cm,gray,very thin}
This is my first plain \TeX\ file. Let me try some commands below:
\vskip 1cm
\center
\tikzpicture[scale=3]
\draw [\sty] (-1.4,-1.4) grid (1.4,1.4);
\draw [->](-1.5,0) -- (1.5,0);
\draw [->](0,-1.5) -- (0,1.5);
\draw (0,0) circle [radius=1cm];
\path[fill,draw][fill=green!20!white, draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
\endtikzpicture
\endcenter

\bye

Maybe it has to do with the order TeX documents are compiled/parsed. I am not sure though.

  • 1
    There really is no reason to do this when you can just define a style. – cfr Apr 24 '16 at 21:02
8

The key-values contained in \sty aren't expanded this way, this causes an error.

You can use \draw \expandafter[\sty]... however

Using a tikzstyle is better, however.

\nopagenumbers
\input tikz.tex

\def\center#1\endcenter{\centerline{#1}}

\def\sty{step=.5cm,gray,very thin,dashed}
This is my first plain \TeX\ file. Let me try some commands below:
\vskip 1cm
\center
\tikzpicture[scale=3]
\draw \expandafter[\sty] (-1.4,-1.4) grid (1.4,1.4);
\draw [->](-1.5,0) -- (1.5,0);
\draw [->](0,-1.5) -- (0,1.5);
\draw (0,0) circle [radius=1cm];
\path[fill,draw][fill=green!20!white, draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
\endtikzpicture
\endcenter

\bye
  • I looked up what \expandafter does online; it delay the expansion of the macro until its argument is expanded. But in this case, what is getting delayed expanding? – Kun Apr 24 '16 at 21:11
  • @Kun The square brackets. – cfr Apr 24 '16 at 21:12
  • I see. I still cannot fully understand the mechanics of how the \tikipicture command is implemented, but thank you. – Kun Apr 24 '16 at 21:19
7

Although it is possible to use a macro as shown in Christian Hupfer's answer, this is not really the best approach when using TikZ, whichever macro format you choose for your document. Instead, it would be better to use TikZ's key-value syntax to create a sty style with \tikzset{}. Note, however, that \tikzstyle is deprecated and ought not be used in new code.

\tikzset{sty/.style={step=.5cm,gray,very thin}}

can be used in the usual way

\tikzset{sty/.style={step=.5cm,gray,very thin}}

sty style application

\nopagenumbers
\input tikz.tex

\def\center#1\endcenter{\centerline{#1}}

\tikzset{sty/.style={step=.5cm,gray,very thin}}
This is my first plain \TeX\ file. Let me try some commands below:
\vskip 1cm
\center
\tikzpicture[scale=3]
\draw [sty] (-1.4,-1.4) grid (1.4,1.4);
\draw [->](-1.5,0) -- (1.5,0);
\draw [->](0,-1.5) -- (0,1.5);
\draw (0,0) circle [radius=1cm];
\path[fill,draw][fill=green!20!white, draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
\endtikzpicture
\endcenter

\bye
5

The macro hides the syntax characters. The comma as separator for the key value pairs and the equals sign need to be visible by the key value parser.

But it is possible to define styles via \tikzstyle (seems to be deprecated, the documentation uses it, but does not explain it) or via the .style handler, e.g.:

\nopagenumbers
\input tikz.tex

\def\center#1\endcenter{\centerline{#1}}

\tikzset{
  mygridstyle/.style={
    step=.5cm,
    gray,
    very thin,
  },
}

This is my first plain \TeX\ file. Let me try some commands below:
\vskip 1cm
\center
\tikzpicture[scale=3]
\draw [mygridstyle] (-1.4,-1.4) grid (1.4,1.4);
\draw [->](-1.5,0) -- (1.5,0);
\draw [->](0,-1.5) -- (0,1.5);
\draw (0,0) circle [radius=1cm];
\path[fill=green!20!white, draw=green!50!black] (0,0) --
(3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
\endtikzpicture
\endcenter

\bye

Result

  • \tikzstyle is deprecated. – cfr Apr 24 '16 at 21:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.