1

I want to color the edge AB with thick blue color and edges AC and BC with thick red color.

Referring to the answer in Multiple color edges in TikZ, one way is to use the \clip[draw] command. But, I am not sure how to use in my context, as my shape is a triangle and the triangle is filled with orange color as well.

Expected Result:

enter image description here

Current Result:

enter image description here

The triangle should still have the orange color filled and the edges with the above mentioned color.

Note: This example is from the PGF manual only. Providing the code in the manual is not recommended/appreciated without explanation.

Is there an alternate way to fill a shape instead of using \filldraw or \draw[fill] command.

MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{through}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[help lines/.style={thin,draw=black!50}]
  \coordinate[label=left:\textcolor{blue!80!black}{$A$}] (A) at (0,0);
  \coordinate[label=right:\textcolor{blue!80!black}{$B$}] (B) at (1.25,0.25);
  \draw[name path=A--B] (A) -- (B);
%  \draw let \p1 = ($(B) - (A)$),
%            \n2 = {veclen(\x1,\y1)}
%        in
%           (A) circle (\n2)
%           (B) circle (\n2);
  \node (D) [name path=D,draw,circle through=(B),label=left:$D$,help lines,draw] at (A) {};
  \node (E) [name path=E,draw,circle through=(A),label=right:$E$,help lines,draw] at (B) {};

  \path [name intersections={of=D and E, by={[label=above:$C$]C,[label=below:$C'$]C'}}];

  %\coordinate[label=above:$C$] (C) at (intersection-1); using by instead

  \draw [name path=C--C',red] (C) -- (C');

  \path [name intersections={of=A--B and C--C', by=F}];
  \node[fill=red,inner sep=1pt, label=-45:$F$] at (F) {};

  \draw[fill=orange!80] (A) -- (B) -- (C) -- cycle;
  \foreach \point in {A,B,C}
    \fill [black,opacity=0.5] (\point) circle (2pt);
\end{tikzpicture}
\end{document} 
  • 2
    What if you add \draw [name path=A--C,thick,red] (A) -- (C) -- (B); \draw [name path=A--B,thick,blue] (A) -- (B); at the end of your tikzpicture? – leandriis Mar 9 at 8:59
  • @leandriis then how do I use fill draw. You are splitting the draw statements – subham soni Mar 9 at 9:30
  • In my above comment I have split up the triangle into three parts (orange triangle, red line, blue line), as you already noticed. In this comment I also made a suggestion on how to draw the orange filled triangle with thick red lines using a single \draw command. You will nevertheless need a second \draw command to add the thick blue line. – leandriis Mar 9 at 9:39
1

Some options can be applied to a portion of the path, this is the case of rounded corners. But this does not work with the colors as indicated on page 149 of manual 3.1.1 which always apply to the entire path.

Screenshot of the page 149:

screenshot

So, to make it easier:

  • I first colored the ABC triangle orange with the \fill command (which doesn't draw anything);
  • then I drew two paths, one in blue, the other in red.

screenshot

Commented code:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{through}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[help lines/.style={thin,draw=black!50}]
  \coordinate[label=left:\textcolor{blue!80!black}{$A$}] (A) at (0,0);
  \coordinate[label=right:\textcolor{blue!80!black}{$B$}] (B) at (1.25,0.25);
  \path[name path=A--B] (A) -- (B);% <-- construct the path, but not draw it
%  \draw let \p1 = ($(B) - (A)$),
%            \n2 = {veclen(\x1,\y1)}
%        in
%           (A) circle (\n2)
%           (B) circle (\n2);
  \node (D) [name path=D,draw,circle through=(B),label=left:$D$,help lines,draw] at (A) {};
  \node (E) [name path=E,draw,circle through=(A),label=right:$E$,help lines,draw] at (B) {};

  \path [name intersections={of=D and E, by={[label=above:$C$]C,[label=below:$C'$]C'}}];

  %\coordinate[label=above:$C$] (C) at (intersection-1); using by instead

  \path [name path=C--C',red] (C) -- (C');

  \path [name intersections={of=A--B and C--C', by=F}];
  %\node[fill=red,inner sep=1pt, label=-45:$F$] at (F) {};

  \fill[orange!80] (A) -- (B) -- (C) -- cycle;%<-- only fill without draw the edges
  \draw[blue,thick](A)--(B);%<-- draw in blue
  \draw[red,thick](A)--(C)--(B);%<-- draw in red
  \foreach \point in {A,B,C}
    \fill [black,opacity=0.5] (\point) circle (2pt);
\end{tikzpicture}
\end{document} 
3

You can save some of your paths by using the possibilities of the \path or \draw command. Especially the empty paths may be removed by integrating the intersections into the relevant paths.

To color your way from A to B simply draw a line between the two points (after drawing the frame or simply removing the frame). The same works for A--B--C.

triangle

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{through}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[help lines/.style={thin,draw=black!50}]
  \path[name path=A--B] (0,0) coordinate[label=left:\textcolor{blue!80!black}{$A$}] (A) --
    (1.25,0.25) coordinate[label=right:\textcolor{blue!80!black}{$B$}] (B);
  \node (D) [name path=D,draw,circle through=(B),label=left:$D$,help lines,draw] at (A) {};
  \node (E) [name path=E,draw,circle through=(A),label=right:$E$,help lines,draw] at (B) {};
  \draw[name intersections={of=D and E, by={[label=above:$C$]C,[label=below:$C'$]C'}},
    name path=C--C',red] (C) -- (C');
  \node[fill=red,inner sep=1pt,name intersections={of=A--B and C--C', by=F},label=-45:$F$] at (F) {};

  \draw[draw=none,fill=orange!80] (A) -- (B) -- (C) -- cycle;
  \draw[blue,thick] (A) -- (B);
  \draw[red,thick] (A) -- (C) -- (B);

  \foreach \point in {A,B,C}
    \fill [black,opacity=0.5] (\point) circle (2pt);
\end{tikzpicture}
\end{document} 
  • One could also use \draw[fill=orange!80, draw=red,thick] (A) -- (B) -- (C) -- cycle; instead of \draw[fill=orange!80] (A) -- (B) -- (C) -- cycle; and \draw[red,thick] (A) -- (C) -- (B);. – leandriis Mar 9 at 9:08
  • @leandriis Well, it's to highlight the "color each edge separately" from the question. You are right that it is even shorter by overlaying drawn paths. – TeXnician Mar 9 at 9:09
  • As you already suggested in your answer to '[...] save some of your paths [...]' I thought the same could also be applied to the code that is added to answer the question. – leandriis Mar 9 at 9:11
  • @leandriis My initial intention was to specifically remove these empty paths, but of course you are right ;) – TeXnician Mar 9 at 9:13

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.