# Tikz: How to indicate/display overlapping lines

I have the following simple picture:

Is there a way to better illustrate the small overlapping interall of LS and LD? I know I could define this area by another line for example and use different dashed version of red and green. But is there a best-practice for all cases of overlapping lines which for example shifts of the lines or splits it automatic into red and green sections? Or at least a color mixture option? Maybe one can use for overlapping areas transparent lines? But this still leaves the question of automatic detection of overlapping issues.

Thank you

\begin{center}
\begin{tikzpicture}[ultra thick]
% Axis and coordinates
\coordinate (y) at (0,5);
\coordinate (o) at (0,0);
\coordinate (x) at (7,0);

% Axis labels, basic line and curve
\draw[<->,line width=1.5pt] (y) node[left, label={[align=left] $w$}] -- (o) -- (x) node[below]{$L^S, L^D$};

\node[left] at (0,0) {0};
\node[left] at (0,1) {z};
\node[left] at (0,4) {$\overline{w}$};

% dashed line and curve
\draw[red] (0,1) -- (1.5,1) node[right, red] {$L^S$};
\draw[red] (1.5,1) -- (3,4);
\draw[red] (3,4) -- (6,4);
\draw[gray, dashed, thin]  (5.5,0) node[below, black] {$\overline{L}$} -- (5.5,4) ;

\draw[ForestGreen] (0,4) -- (3.5,4);
\draw[ForestGreen] (3.5,4) -- (5,1) node[left, ForestGreen] {$L^D$};

\end{tikzpicture}
\end{center}

• If you drew the line underneath with a thicker pen it would show more clearly. – Thruston Nov 19 '15 at 15:55
• That's not really a question about TeX or LaTeX, is it? The first question is, how do you want things to look like? Only then can you think about how to get there using tikz. – jarauh Nov 19 '15 at 17:02
• By the way, I would say that the figure is clear to me as it is. If you drew the line underneath with a thicker pen, then you would either have to draw it thicker everywhere, or there would be a change in thickness. If you really want to have a clear picture, use two figures. – jarauh Nov 19 '15 at 17:04
• @jarauh What else do you need for a question? – percusse Nov 19 '15 at 17:17
• Why is this even voted for closing? – percusse Nov 19 '15 at 19:59

Inspired by "Z-level" in TikZ

\pgfdeclarelayer{front}
\pgfdeclarelayer{back}
\pgfsetlayers{back,main,front}
\tikzset{% https://tex.stackexchange.com/questions/20425/z-level-in-tikz
on layer/.code={\pgfonlayer{#1}\tikzset{every picture}\begingroup\aftergroup\endpgfonlayer\aftergroup\endgroup},
also in front/.style 2 args={#1,postaction={on layer=front,draw,#1,#2}},
also behind/.style 2 args={#1,postaction={on layer=back,draw,#1,#2}},
every picture/.style={line width=6},
}

\tikz{
\draw[also in front={red}{opacity=.5}](-2,0)--(1,0)--(2,1);
\draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
\draw[also in front={red}{dashed}](-2,0)--(1,0)--(2,1);
\draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
\draw[also in front={red}{line width=.5\pgflinewidth}](-2,0)--(1,0)--(2,1);
\draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
\draw[also in front={red}{line width=.5\pgflinewidth}](-2,0)--(1,0)--(2,1);
\draw[also behind={}{green},also in front={green,dashed}{line width=.5\pgflinewidth,dash phase=3pt}](-2,-1)--(-1,0)--(2,0);
}

\tikz{
\let\tikzerror\relax
\draw[also in front={red}{clip,draw=none,postaction={draw}}](-2,0)--(1,0)--(2,1);
\draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
\let\tikzerror\relax
\path(-2,-1)(2,1);
\draw[also behind={}{red},preaction={draw=red,dashed},also in front={clip,draw=none}{postaction={draw=red,dashed,dash phase=3pt}}](-2,0)--(1,0)--(2,1);
\draw[also behind={green}{}](-2,-1)--(-1,0)--(2,0);
}


and a crazy one. (basically copy the definition of snake)

\pgfdeclaredecoration{DNA}{initial}
{
\state{initial}[switch if less than=+.625\pgfdecorationsegmentlength to final,
width=+.3125\pgfdecorationsegmentlength,
next state=down]
{
\pgfpathcurveto
{\pgfqpoint{.125\pgfdecorationsegmentlength}{0pt}}
{\pgfqpoint{.1875\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
{\pgfqpoint{.3125\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
}
\state{down}[switch if less than=+.8125\pgfdecorationsegmentlength to end down,
width=+.5\pgfdecorationsegmentlength,
next state=up]
{
\def\pgfpathcurveto##1##2##3{\pgfpathmoveto{##3}}
\pgfpathcosine{\pgfqpoint{.25\pgfdecorationsegmentlength}{-1\pgfdecorationsegmentamplitude}}
\pgfpathsine{\pgfqpoint{.25\pgfdecorationsegmentlength}{-1\pgfdecorationsegmentamplitude}}
}
\state{up}[switch if less than=+.8125\pgfdecorationsegmentlength to end up,
width=+.5\pgfdecorationsegmentlength,
next state=down]
{
\pgfpathcosine{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
\pgfpathsine{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
}
\state{end down}[width=+.3125\pgfdecorationsegmentlength,
next state=final]
{
\pgfpathcurveto
{\pgfqpoint{.125\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
{\pgfqpoint{.1875\pgfdecorationsegmentlength}{0pt}}
{\pgfqpoint{.3125\pgfdecorationsegmentlength}{0pt}}
}
\state{end up}[width=+.3125\pgfdecorationsegmentlength,
next state=final]
{
\pgfpathcurveto
{\pgfqpoint{.125\pgfdecorationsegmentlength}{-\pgfdecorationsegmentamplitude}}
{\pgfqpoint{.1875\pgfdecorationsegmentlength}{0pt}}
{\pgfqpoint{.3125\pgfdecorationsegmentlength}{0pt}}
}
\state{final}
{
\pgfpathlineto{\pgfpointdecoratedpathlast}
}
}
\tikz[line width=2]{
\draw[red,decorate,decoration={snake}](-2,0)--(1,0)--(2,1);
\draw[green,decorate,decoration={snake}](-2,-1)--(-1,0)--(2,0);
\draw[red,decorate,decoration={DNA}](-2,0)--(1,0)--(2,1);
}


Just before this question got closed, here is a TikZ-specific approach inspired by this:

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\pgfdeclaredecoration{penciline}{initial}{
\state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,auto corner on length=1mm,]{
\pgfpathcurveto%
{% From
\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
{\pgfdecorationsegmentamplitude}
}
{%  Control 1
\pgfmathrand
{\pgfqpoint{-\pgfdecorationsegmentaspect\pgfdecoratedinputsegmentremainingdistance}%
{\pgfmathresult\pgfdecorationsegmentamplitude}
}
}
{%TO
}
}
\state{final}{}
}
\begin{tikzpicture}[ultra thick,decoration=penciline]
% Axis and coordinates
\coordinate (y) at (0,5);
\coordinate (o) at (0,0);
\coordinate (x) at (7,0);

% Axis labels, basic line and curve
\draw[<->,line width=1.5pt] (y) node[left, label={[align=left] $w$}]{} -- (o) -- (x) node[below]{$L^S, L^D$};
\node[left] at (0,0) {0};
\node[left] at (0,1) {z};
\node[left] at (0,4) {$\overline{w}$};
% dashed line and curve
\draw[red,decorate] (0,1) -- (1.5,1)node[right, red] {$L^S$}(1.5,1) -- (3,4) -- (6,4);
\draw[gray, dashed, thin]  (5.5,0) node[below, black] {$\overline{L}$} -- (5.5,4) ;
\draw[green,decorate] (0,4) -- (3.5,4) -- (5,1) node[left, green] {$L^D$};
\end{tikzpicture}
\end{document}

• The wavy lines with the straight axes look really... not good. Probably go XKCD all the way? – Tom Bombadil Nov 19 '15 at 19:07
• @TomBombadil It is OK to vote down I guess :P I was thinking about people drawing lines on sheets with given axes, hence straight. The point is: if lines are straight but perturbed, readers would notice the difference and consider they are in fact different. (Recall the 29002ft story.) But if lines are of xkcd style, readers would think they are intends to drawn overlayed. – Symbol 1 Nov 19 '15 at 19:16
• No, that's not what I meant! While looking not that nice, it provides a solution to the question, so there will be no downvote from me. But then again, I can't really see myself upvoting, sorry for that. I think I'd go with different widths or linestyles or transparency. – Tom Bombadil Nov 19 '15 at 19:28
• @TomBombadil Transparency is the best candidate I would say. I came up with transparency at the first moment as well. After all, I chose to post this TikZ-specific answer against the close votes. – Symbol 1 Nov 19 '15 at 19:36
• To quote Unreal Tournament: Wicked Sick! Now I'm sad that I can't upvote twice! – Tom Bombadil Nov 21 '15 at 19:12

For overlapping lines, I would recommend using different styles such as dotted or dashed:

## Note:

• As this is a graph you really should consider using pgfplots instead of straight tikz.

## Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[ultra thick]
% Axis and coordinates
\coordinate (y) at (0,5);
\coordinate (o) at (0,0);
\coordinate (x) at (7,0);

% Axis labels, basic line and curve
\draw[<->,line width=1.5pt] (y)
node[left, label={[align=left] $w$}] {} -- (o) -- (x)
node[below]{$L^S, L^D$};

\node[left] at (0,0) {0};
\node[left] at (0,1) {z};
\node[left] at (0,4) {$\overline{w}$};

% dashed line and curve
\draw[red] (0,1) -- (1.5,1) node[right, red] {$L^S$};
\draw[red] (1.5,1) -- (3,4);
\draw[red] (3,4) -- (6,4);
\draw[gray, dashed, thin]  (5.5,0) node[below, black] {$\overline{L}$} -- (5.5,4) ;

\draw[blue, dotted] (0,4) -- (3.5,4);
\draw[blue, dotted] (3.5,4) -- (5,1) node[left, blue] {$L^D$};

\end{tikzpicture}
\end{document}