5

How can be colored a line/path in tikz? i.e. green before the 5 and red after? enter image description here

I have seen a option with

\usetikzlibrary{decorations.markings}

but this seems too much complex to the task (even though I couldn't do it). But is the only way?

\begin{tikzpicture}[scale=0.5]
    \draw[ blue] (-6,0)-- (9,0); 
    \foreach \x in {0,5} {
        \draw (\x,0.5) -- (\x,-0.5) node[below] {\x};
    }
    \draw[] (5,0) circle (0.5);
   
    \end{tikzpicture}
2

1 Answer 1

4

I think edge[] is what you are looking for:

\documentclass{article}
\usepackage{tikz}


\begin{document}
    
    \begin{tikzpicture}[scale=0.5]
        \draw[ blue] (-6,0)-- +(0:11cm) edge[green] +(0:6cm) edge[red] (9,0); 
        \foreach \x in {0,5} {
            \draw (\x,0.5) -- (\x,-0.5) node[below] {\x};
        }
        \draw[] (5,0) circle (0.5);
        
    \end{tikzpicture}
    
\end{document}

enter image description here

2
  • Thanks, how do you adjust the start/end of the color ?, +(0:11cm) edge[green] means to start in (0,11) or from the value of 0 to the 11 over x?
    – riccs_0x
    Dec 14, 2020 at 2:06
  • 1
    Here you will find a good explanation what edge does.
    – Roland
    Dec 14, 2020 at 2:51

You must log in to answer this question.

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