11

I frequently use every node/.style={blue,draw} to set style for every node as in the MWE below

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[every node/.style={blue,draw}]
    \node {Text};
    \end{tikzpicture}
\end{document}

I have tried to apply this method to draw not node, but I failed to it. Here is my code

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[every draw/.style={->,red,thick}]
    \draw(0,0)--(5,0);
    \end{tikzpicture}
\end{document}

How can we achieve this?

4 Answers 4

13

There is no every draw available. One possible way is to style every path.

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[every path/.style={->,red,thick}]
    \draw(0,0)node[left]{$ A $}--(5,0)node[right]{$ B $};
    \end{tikzpicture}
\end{document}

enter image description here

An alternative solution is to globally set draw for every picture. In this way, color specification does not affect node!

\documentclass{article}
\usepackage{tikz}
\tikzset{every picture/.style={->,draw=red,thick}}
\begin{document}
    \begin{tikzpicture}
    \draw(0,0)node[left]{$ A $}--(5,0)node[right]{$ B $};
    \end{tikzpicture}
\end{document}

enter image description here

3
  • 1
    This will also affect nodes. Apr 7, 2016 at 7:36
  • @TorbjørnT. Yep! you are right. But it seems there is no easy way to style only draw.
    – Say OL
    Apr 7, 2016 at 7:47
  • 1
    I don't know of any, but there might be a way. A workaround would be to place all the \draws in a scope environment and add the every path style to the scope options. Apr 7, 2016 at 7:58
3

As other answers have said, you can set every path, but that changes the node styles. One way around this is to set every node just afterwards:

Sample output

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[every path/.style={->,red,thick},
  every node/.style={draw,blue,thin}]
  \draw(0,0)--(5,0) node[rectangle] {XX} -- (5,-1);
\end{tikzpicture}

\end{document}
1
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[>=stealth, every node/.style={midway, sloped, font=\tiny},
decoration={show path construction,
lineto code={
\draw [red, thick,->] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);},}]
\path [decorate] (0,0) -- (3,1);
\end{tikzpicture}
\end{document}

enter image description here

3
  • You cant use node since you set "draw style". I suggest to use tikzlibrary decorations.pathreplacing to set the decoration "path construction". More info see in tikz-pgf manual by T. Tantau
    – Olga K
    Apr 7, 2016 at 5:39
  • I got the idea now! Thank you for your pointing out. Actually, I misunderstood about key-val in tikz-pgf. I can just change every draw/.style={->,red,thick} to every path/.style={->,red,thick}
    – Say OL
    Apr 7, 2016 at 5:51
  • Ok, if you dont mind, show your solution here. Will be helpful for people)
    – Olga K
    Apr 7, 2016 at 5:55
1

Following up on the comment above, here is a demo using every path in a scope environment to achieve a style effect on a group of paths, including nodes. Note the draw operations before and after the scope get default styling.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\draw (0,0) rectangle ++(4,4);

\begin{scope}[every path/.style={draw, line width=2.0mm, color=red}]
    \draw (1,1) -- ++(4,0);
    \draw (2,2) rectangle ++(3,3);
    \node at (3,3) {Narf};
\end{scope}

\draw (3,3) -- ++(4,0);
\node[draw] at (6,6) {Poit};

\end{tikzpicture}
\end{document}

To apply this technique and have the style apply to draw but not node, put all draw commands within the scope and keep all node commands outside the scope.

scope example

6
  • Welcome to TeX - LaTeX! As this is still styling the nodes, I don't see how this helps the OP. Jun 28, 2017 at 15:31
  • @AndrewSwann The point is to be able to write your style info once using every path, instead of having to re-write it for every path/node. But by organizing the code using scope, you can also keep some paths/nodes with your default style. In other words, this doesn't style any nodes that are outside the scope.
    – SpinUp
    Jun 28, 2017 at 15:38
  • I'm sure it's a very simple example if you've been using tikz for any length of time, but this is all quite new to me.
    – SpinUp
    Jun 28, 2017 at 15:40
  • @AndrewSwann I have added a \node to the example, specifically outside the scope, to show that not all nodes get the style using this technique.
    – SpinUp
    Jun 28, 2017 at 15:49
  • I agree with you that scoping is useful, but it doesn't answer the OP's question. Jun 28, 2017 at 16:40

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.