How to add multiple differently colored borders around a node?
Like this?
\documentclass[dutch]{article}
\usepackage{babel}
\usepackage{tikz}
\usetikzlibrary{fit}
\definecolor{lichtgrijs}{RGB}{232,232,232}
\definecolor{DE.rood}{RGB}{222,0,0} % Rood in Duitse vlag
\definecolor{DE.geel}{RGB}{255,207,0} % Geel in Duitse vlag
\begin{document}
\begin{tikzpicture}[
box/.style = {draw=#1, line width=0.5mm,inner sep=0.25mm}
]
\node (n1) [box=DE.geel,
fill=lichtgrijs, inner sep=2mm] at (0,0) {Vliegtuig};
\node (n2) [box=DE.rood, fit=(n1)] {};
\node (n3) [box=black, fit=(n2)] {};
\end{tikzpicture}
\end{document}
Some Notes:
- never nest
tikzpicture
intikzpicture
, which may raise unexpected side effects; - the
fit
library oftikz
is used to get what you want; - you can define node style, which merges all nodes in one, but I'd like first to know whether the illustrated result is what you're looking for :-)
addendum:
here is a three-color node variation. In particular, append after command=
option in a node style is used for the middle and the outer colors:
\documentclass[dutch]{article}
\usepackage{babel}
\usepackage{tikz}
\usetikzlibrary{fit}
\definecolor{lichtgrijs}{RGB}{232,232,232}
\definecolor{DE.rood}{RGB}{222,0,0} % Rood in Duitse vlag
\definecolor{DE.geel}{RGB}{255,207,0} % Geel in Duitse vlag
\begin{document}
\begin{tikzpicture}[
tcb/.style = {% three color border
draw=DE.geel, fill=lichtgrijs,
line width=0.5mm,inner sep=2mm,
append after command={\pgfextra{\let\LN\tikzlastnode
\node [draw=DE.rood, line width=0.5mm,
inner sep=0.25mm,fit=(\LN)] {};
\node [draw, line width=0.5mm,
inner sep=0.75mm,fit=(\LN)] {};
}}}
]
\node (n1) [tcb] {Vliegtuig};
\end{tikzpicture}
\end{document}
The result is the same as before.
Just for fun:
\documentclass{standalone}% to avoid cropping
\usepackage{babel}% not needed for MWE
\usepackage{tikz}
\usetikzlibrary{calc}
\definecolor{lichtgrijs}{RGB}{232,232,232}
\definecolor{DE.rood}{RGB}{222,0,0}
\definecolor{DE.geel}{RGB}{255,207,0}
\begin{document}
\begin{tikzpicture}
\begin{scope}[line width=0.5mm]
\node[draw=black,inner sep = 1.5mm] (n1) at (0,0) {Vliegtuig};
\draw[DE.rood] ($(n1.south west)+(0.75mm,0.75mm)$) rectangle ($(n1.north east)+(-0.75mm,-0.75mm)$);
\draw[DE.geel] ($(n1.south west)+(1.25mm,1.25mm)$) rectangle ($(n1.north east)+(-1.25mm,-1.25mm)$);
\end{scope}
\end{tikzpicture}
\end{document}
Without TikZ, only \colorbox
es.
I've added \usepackage{picture}
to use the widths/lengths not specified in multiples of \unitlength
.
I've added \usepackage{calc}
for convenience, to do calculations in \setlength
(you can avoid it if you use \addtolength
).
I've used \makebox(\myxxxwidth, \myxxxheight){...}
to specify the width and total height of the boxes.
I've calculated the width of the boxes this way:
width of the border + width of the content + width of the border
that is
width of the content + double of the width of the border
You can set the border widths as you like.
The same for heights.
I've created a new command \mybox
for convenience, you can even modify it to set the border widths as a parameter/parameters, if you like.
\documentclass[pdftex,dutch]{article}
\usepackage{babel}
\usepackage{xcolor}
\usepackage{picture}% see here: https://tex.stackexchange.com/a/48238/101651
\usepackage{calc}
\newlength{\mygraywidth}
\newlength{\mygrayheight}
\newlength{\myyellowwidth}
\newlength{\myyellowheight}
\newlength{\myredwidth}
\newlength{\myredheight}
\newlength{\myblackwidth}
\newlength{\myblackheight}
\definecolor{lichtgrijs}{RGB}{232,232,232}
\definecolor{DE.rood}{RGB}{222,0,0}
\definecolor{DE.geel}{RGB}{255,207,0}
\newcommand{\mybox}[1]{%
\setlength{\mygraywidth}{\widthof{#1}+4mm}
\setlength{\mygrayheight}{\totalheightof{#1}+4mm}
\setlength{\myyellowwidth}{\mygraywidth+1mm}
\setlength{\myyellowheight}{\mygrayheight+1mm}
\setlength{\myredwidth}{\myyellowwidth+1mm}
\setlength{\myredheight}{\myyellowheight+1mm}
\setlength{\myblackwidth}{\myredwidth+1mm}
\setlength{\myblackheight}{\myredheight+1mm}
\colorbox{black}{\makebox(\myblackwidth,\myblackheight){%
\colorbox{DE.rood}{\makebox(\myredwidth,\myredheight){%
\colorbox{DE.geel}{\makebox(\myyellowwidth,\myyellowheight){%
\colorbox{lichtgrijs}{\makebox(\mygraywidth, \mygrayheight){%
#1}}%
}}%
}}%
}}%
}
\begin{document}
\mybox{Vliegtuig}
\vspace{4ex}
\mybox{Do you like it?}
\end{document}