0

I'd like to define options via newcommand, so i can use them for tikzpicture-environments. I tried this:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\newcommand{\test}{[every path/.style={->,red,thick}]}
\begin{document}
\begin{tikzpicture}\test
\draw (0,0)--(1,1);
\end{tikzpicture}
\end{document}

But the line is still black. Where is the mistake in my approach? i thought, newcommand simply expands its content when it is written. I assume that the square brackets are perhaps the problem...

1
  • 1
    Try \newcommand{\test}{ \tikzset{every path/.style={->,red,thick}}}
    – user31034
    Commented Jan 28, 2020 at 12:44

2 Answers 2

1

Welcome to TeX.SE! While technically correct, the method given in @ferahfeza's comment is probably not the one I would use here. Staying in the spirit of pgfkeys, I would define a /tikz/test style. This is often easier to reuse within TikZ pictures. Note that both methods allow one to take advantage of TeX's grouping rules, as shown below in the case of “my” method:

\documentclass{article}
\usepackage{tikz}

\tikzset{
  test/.style={
    every path/.style={->, red, thick},
  }
}

\begin{document}

\begin{tikzpicture}[test] % '/tikz/test' style manually set for this picture
  \draw (0,0)--(1,1);
\end{tikzpicture}

\bigskip
{%
  \tikzset{test}% Applies to the current TeX group, until (possibly) overridden
  %
  \begin{tikzpicture}
    \draw (1,1)--(0,0);
  \end{tikzpicture}%
  \qquad
  \begin{tikzpicture}
    \draw (0,1)--(1,0);
  \end{tikzpicture}%
}% TeX group terminated; the '/tikz/test' style is not in effect anymore.

\bigskip
\begin{tikzpicture}
  \draw (0,0)--(1,1) (1,0)--(0,1);
\end{tikzpicture}

\end{document}

screenshot

0

In the spirit of NOT using \pgfkeys, one can use

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\newcommand{\test}{[every path/.style={->,red,thick}]}
\newcommand{\tikzstart}{\begin{tikzpicture}}
\begin{document}
\expandafter\tikzstart\test
\draw (0,0)--(1,1);
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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