39

I want to draw a line or path with dashed line style at its begining and a solid line at the end, but in only one path (I mean, not drawing two path), something like :

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (3,0) -- (1,0)[dashed] -- (0,0)[solid] ;
\end{tikzpicture}
\end{document}
7
  • 2
    It's not possible to change the style of a path mid-way like this. There are ways to simulate it, though, by breaking the path into two (in such a way that you could still write something similar to the above). Would one of those be acceptable? Feb 16, 2012 at 10:04
  • @AndrewStacey: You mean it can work using \draw [dashed] (3,0) -- (1,0) ; \draw (1,0) -- (0,0)[solid] ; ? Yes, but the junction of the lines is not as clean as if the line is define in one command (the first dash is closer to the solid line than to the second dash). This is the reason why I ask for a complete command... But maybe there is other ways that can solve the problem.
    – Sigmun
    Feb 16, 2012 at 10:15
  • Maybe one could define the dash pattern carefully to get the join right. It would depend on exactly what join you wanted to achieve. Feb 16, 2012 at 10:30
  • 1
    I don't understand the problem. If you use two separate commands (\draw (0,0) -- (1,0);\draw[dashed](1,0) -- (2,0);) The first dashed part is connected to the solid line. What is it you want to achieve exactly? Feb 16, 2012 at 10:43
  • @Sigmun Sorry I don't read correctly the question. I saw line instead of path also my answer is not really appropriated to your question. It's possible to begin the dash line with pos=... but it's very complicated Then the junction of the lines will be not very clean and it's what you want. If you want a good junction you can draw like this :\draw [dashed] (1,0) -- (3,0) ; \draw (1,0) -- (0,-1)[solid] ; You need to draw from the corner. The problem with the dash pattern is complicated. Feb 16, 2012 at 16:00

2 Answers 2

29

If those are to be straight line segments, it couldn't get any simpler. It gets interesting with curves. Enclosed are a few examples, and I agree - it's too complicated. Clipping is the most accurate way to tell where the solid part should stop, but it requires its own scope. Fading is good for smooth transition but probably would only give headache if the path doesn't run more or less in one direction. enter image description here

I'm quite sure there is a way to have a procedural stroke of the path, but I haven't found any information about that so far. I would personally enjoy a simple style that allows to easily morph the line type from start until end of a path, doing something similar to the second custom dash pattern but without all the typing. But for now, this is what I did:

\documentclass[12pt,a4paper]{minimal}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{document}
\begin{tikzpicture}[line width=1.5pt]


\node at (200pt,40pt) [label=right:{2 line segments}]{};
% for straight segments this is the easiest:
%
\draw[dashed] (200pt,40pt) -- (60pt,40pt);
\draw (60pt,40pt) -- (0pt,40pt);


\node at (200pt,10pt) [label=right:{custom dash pattern}]{};
% for curves you can specify custom dash pattern, but it's cumbersome 
% having to specify every singly dash untill the line becomes solid:
%
\draw[dash pattern=on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 10pt off 10pt on 100pt] (200pt,10pt) .. controls (40pt,40pt)  and (90pt,-30pt) .. (0,20pt);


\node at (200pt,-10pt) [label=right:{custom dash pattern}]{};
% it does however allow for nice density modifications (but also by hand):
%
\draw[dash pattern=on 10pt off 9pt on 9pt off 8pt on 8pt off 7.3pt on 7.3pt off 6.7pt on 6.7pt off 6.3pt on 6.3pt off 5.9pt on 5.9pt off 5.4pt on 5.4pt off 5.0pt on 5.0pt off 4.0pt on 4.5pt off 3.0pt on 4.0pt off 2.4pt on 3.5pt off 1.8pt on 100pt] (200pt,-10pt) .. controls (40pt,20pt)  and (90pt,-50pt) .. (0,0pt);


\node at (200pt,-30pt) [label=right:{2 curves: dashed $+$ clipped solid}]{};
% now there's 2 paths - a long dashed one and a clipped solid one:
%
\begin{scope}
\clip (-5pt,-70pt) rectangle (60pt,5pt);
\draw (200pt,-30pt) .. controls (40pt,0pt)  and (90pt,-70pt) .. (0,-20pt);
\end{scope}
\draw[dashed] (200pt,-30pt) .. controls (40pt,0pt)  and (90pt,-70pt) .. (0,-20pt);


\node at (200pt,-50pt) [label=right:{2 curves: dashed $+$ faded solid}]{};
% for a gradient toning down of the solid line you can use fading:
%
\draw[path fading=east] (200pt,-50pt) .. controls (40pt,-20pt) and (90pt,-90pt) .. (0,-40pt);
\draw[dashed,path fading=west] (200pt,-50pt) .. controls (40pt,-20pt) and (90pt,-90pt) .. (0,-40pt);


\end{tikzpicture}
\end{document}
3
  • 2
    The clipped solid solution doesn't allow a nice junction between solid and dashed lines: the tail of the solid line is clip vertically due to the clip command. I also improve a bit the faded solution by fading also the dashed line in the other way.
    – Sigmun
    Feb 18, 2012 at 16:58
  • Good idea with also fading out the dashed curve. Now the left part looks smoother. As for clip, a shape different than the orthogonal rectangle would naturally give better results.
    – Frg
    Feb 18, 2012 at 18:15
  • You right, rectangle could be something else but this becomes hard to setup automagically !
    – Sigmun
    Feb 22, 2012 at 15:25
4

Another way (partially automated and gives you control over the amount of dashes):

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{figure}[h!]
\begin{tikzpicture}[every path/.style={>=latex}]

% Normal dashed line
\draw[thick,black] (0,1) -- (1,1);
\draw[->,thick,black,dashed] (1,1) -- (4,1) node[anchor=west] {Normal dashed line};

% Specify dashed line starting coordinate and length
\draw[thick,black] (0,0) -- (1,0);
\def\x{1}; \def\y{0}; \def\length{3} \def\N{4}; 
% Draw dashed line using normal lines
\pgfmathsetmacro{\step}{(0.5+1/(4*\N))*\length/\N}; \pgfmathparse{\N-1};
\foreach \i in {0,...,\pgfmathresult} {\draw[thick,black] (\x+2*\i*\step,\y) -- (\x+2*\i*\step+\step,\y);}; 
\draw[->,thick,black] (\x+\length,\y) -- (\x+\length+0.01,\y) node[anchor=west] {Custom dashed line};

\end{tikzpicture}
\end{figure}
\end{document}

This simply mimicks a normal dashed line by drawing multiple lines after each other. Parameters are \x for the starting x-coordinate, \y for the y-level of the line, \length for the length in coordinates of the line and \N for the amount of stripes.

arrows

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.