5

I want to have a node with different border colors: One for the upper and left border, one for the lower and right color. The result should look like this:

http://flyx.org/files/tikznode.png

Is this possible? From all examples I found, it seems that I can only set the color as a whole, not for each side.

  • There is a duplicate of this but I don't know what to search for :) – percusse Sep 7 '14 at 12:07
  • I didn't either. – flyx Sep 7 '14 at 12:10
  • 1
    @percusse This: Tikz shape similar to rectangle with selective drawing of borders ? – user11232 Sep 7 '14 at 12:17
  • Is drawing separately allowed? – user11232 Sep 7 '14 at 12:19
  • @HarishKumar Sure, but it should be reproducible with arbitrary node sizes. I thought about drawing two triangles in the background and setting the node above them using the fit package, but I guess I would get problems with deeper nesting when I do this. – flyx Sep 7 '14 at 12:32
6

This should get you started. More polishing can be done, but left as an exercise.

\documentclass{article}
\usepackage{tikz}
\tikzset{%
pics/.cd,
mynode/.style args={#1#2#3}{
  code={\node (a) {#3};
       \draw[thick,#1] (a.south west) |- (a.north east);
       \draw[thick,#2] (a.south west) -| (a.north east);
  }
},
}
\begin{document}
  \begin{tikzpicture}
    \pic {mynode={red}{blue}{Here}};
    \pic at (4,0) {mynode={olive}{magenta}{Here comes another one}};
  \end{tikzpicture}
\end{document}

enter image description here

\documentclass{article}
\usepackage{tikz}
\tikzset{%
pics/.cd,
mynode/.style args={#1#2#3}{
  code={\node (a) {#3};
       \draw[thick,#1,line cap=butt,shorten <= -0.5\pgflinewidth,shorten >= 0.5\pgflinewidth] (a.south west) |- (a.north east);
       \draw[thick,#2,line cap=butt,shorten <= 0.5\pgflinewidth,shorten >= -0.5\pgflinewidth] (a.south west) -| (a.north east);
  }
},
}
\begin{document}
  \begin{tikzpicture}
    \pic {mynode={red}{blue}{Here}};
    \pic at (4,0) {mynode={olive}{magenta}{Here comes another one}};
  \end{tikzpicture}
\end{document}

enter image description here

  • Thanks! I'm not sure what to do about the ugly corners where the colors meet, but I'll try to fix that myself. – flyx Sep 7 '14 at 12:46
  • @flyx I have added one option for it. – user11232 Sep 7 '14 at 13:00
  • Your code may be correct mathematically, but it produces a small gap between the lines when I render it. I could use a factor slightly smaller than 0.5, but the gap has a different width depending on the zoom level, so this is not the solution. I need a way to actually connect the lines with each other. – flyx Sep 9 '14 at 18:44

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.