Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a drawing in TiKZ (of a graph, say) and I want to highlight two paths in the drawing. Is there a way to do that using my already-defined node names, without explicitly making new coordinates?

I can almost get there by making my extra paths go through node anchors, but I'd like to offset the paths farther away from the nodes than where the anchors are.

(I've attached a minimal example below. Ideally, I'd like both the red and blue paths to be on the outside of the triangle.)

\documentclass{article} 
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc, through}
\tikzstyle{vertex}=[circle, draw, inner sep=1pt, minimum size=6pt, fill=black!20]

\begin{document}    
\begin{tikzpicture}
\foreach \i in {0,1,2}{\node[vertex] (\i) at (\i*120:1) {};}
\node[vertex] (4) at (2,0){};
\draw (0) --(1) --(2) -- (0);
\draw[dashed] (0) -- (4);
\draw[red, rounded corners] (4.north) -- (0.north) -- (1.north east) --(1.west) -- (2.west); 
\draw[blue, rounded corners] (1.south east) -- (2.south east) -- (0.south) --(4.south); 
\end{tikzpicture}
\end{document}

mwe image

share|improve this question
    
Similar to tex.stackexchange.com/questions/27171/…? –  percusse Apr 16 at 20:14
1  
You can use already existing coordinates and shift them according your needs: draw ([yshift=3pt]4.north)--([yshift=3pt]0.north) ... . You can use xshift=..., yshift=... or shift={(...,...)} (don't forget { and } with last one). –  Ignasi Apr 22 at 13:17

1 Answer 1

up vote 6 down vote accepted

If you put both on the outside, one with partially overwrite the other, won't it?

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc, through}
\tikzstyle{vertex}=[circle, draw, inner sep=1pt, minimum size=6pt, fill=black!20]

\begin{document}
\begin{tikzpicture}
\foreach \i in {0,1,2}{\node[vertex] (\i) at (\i*120:1) {};}
\node[vertex] (4) at (2,0){};
\draw (0) --(1) --(2) -- (0);
\draw[dashed] (0) -- (4);
\draw[red, rounded corners=1pt] (4.north) -- (0.north)-- (1.north east) -- (1.north) --(1.north west) -- (1.west) -- (2.west);
\draw[blue, rounded corners=1pt] (1.west) -- (2.west) -- (2.south west) -- (2.south) -- (2.south east) -- (0.south) --(4.south);
\end{tikzpicture}
\end{document}

Triangle

So you could use the positioning library to fine tune the spacing and avoid the second path overwriting the first:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc, through, positioning}
\tikzstyle{vertex}=[circle, draw, inner sep=1pt, minimum size=6pt, fill=black!20]

\begin{document}
\begin{tikzpicture}[node distance=.1mm]
\foreach \i in {0,1,2}{\node[vertex] (\i) at (\i*120:1) {};}
\node[vertex] (4) at (2,0){};
\draw (0) --(1) --(2) -- (0);
\draw[dashed] (0) -- (4);
\draw[red, rounded corners=1pt] (4.north) -- (0.north)-- (1.north east) -- (1.north) --(1.north west) -- (1.west) -- (2.west);
\node (a) [left=of 1.west] {};
\node (b) [left=of 2.west] {};
\path [draw, blue, rounded corners=1pt] (a.east) -- (b.east) -- (2.south west) -- (2.south) -- (2.south east) -- (0.south) --(4.south);
\end{tikzpicture}
\end{document}

Distinct paths

or, setting the node distance=.25mm:

More distinct paths

To change the node distance only locally so that it does not affect the location of other nodes in a complex diagram, you can limit the scope of the setting. For example, in this diagram, I limit the setting of node distance=.25mm and illustrate this by placing another node z after the scope of the setting has ended:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc, through, positioning}
\tikzstyle{vertex}=[circle, draw, inner sep=1pt, minimum size=6pt, fill=black!20]

\begin{document}
\begin{tikzpicture}
\foreach \i in {0,1,2}{\node[vertex] (\i) at (\i*120:1) {};}
\node[vertex] (4) at (2,0){};
\draw (0) --(1) --(2) -- (0);
\draw[dashed] (0) -- (4);
\draw[red, rounded corners=1pt] (4.north) -- (0.north)-- (1.north east) -- (1.north) --(1.north west) -- (1.west) -- (2.west);
{\tikzset{node distance=.25mm}
\node (a) [left=of 1.west] {};
\node (b) [left=of 2.west] {};
\path [draw, blue, rounded corners=1pt] (a.east) -- (b.east) -- (2.south west) -- (2.south) -- (2.south east) -- (0.south) --(4.south);}
\node (c) [left=of 1.west] {z};
\end{tikzpicture}
\end{document}

z to left with standard setting of node distance

Note that the only difference between

\node (a) [left=of 1.west] {};

and

\node (c) [left=of 1.west] {z};

as regards location is that the former is within the scope of \tikzset{node distance=.25mm} whereas the latter is not and therefore uses the default setting.

share|improve this answer
    
Yes. I need them to not overlap---I need to be able to see both paths. –  Leah Wrenn Berman Apr 16 at 23:24
    
@LeahWrennBerman See edits. I think the slightly larger spacing is preferable but you can obviously adjust to suit. –  cfr Apr 17 at 0:15
    
That's a nice solution---and your using all the anchors to go around the node is helpful---but it doesn't really play nice with other relatively-positioned nodes on more complicated graphs. :( –  Leah Wrenn Berman Apr 17 at 21:53
    
@LeahWrennBerman Well I changed node distance for the whole picture but if you have other relatively-positioned nodes obviously you wouldn't want to do that and would instead specify it locally, wouldn't you? –  cfr Apr 17 at 21:59
1  
@LeahWrennBerman See my edit. Does that help at all? The z is just to demonstrate that the default node distance isn't affected. –  cfr Apr 18 at 19:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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