# TikZ: halo around text?

I'd like to label my nodes with text that has a 'halo' around it of a different colour. Is this possible?

The reason is that my label text covers different parts of my diagram with different background colours, so no single colour will make the label legible. I'd ideally like the text to be black, with a white halo.

-

You can use the contour package with the outline option to create a thick outline around your text. Note that this won't work with things like fraction lines or the horizontal lines of root symbols (see How can I put a coloured outline around fraction lines?).

Andrey Vihoy mentioned the option of just filling the node background. I've added this approach for comparison.

Andrew Stacey suggests rounding the corners of the filled node, which does indeed look more elegant. This variation is shown here as well.

\documentclass{article}

\usepackage{tikz}
\usepackage[outline]{contour}
\contourlength{1.2pt}

\begin{document}
\begin{tikzpicture}
\draw [fill=gray!10!white](0,0) rectangle (4.5,1) [step=0.1cm] (0,0) grid (4.5,1);
\node at (0.75,0.5) {\contour{white}{\Large Text!}};
\node [fill=white,inner sep=1pt] at (2.25,0.5) {\Large Text!};
\node [fill=white,rounded corners=2pt,inner sep=1pt] at (3.75,0.5) {\Large Text!};
\end{tikzpicture}
\end{document}


-
I didn't know the contour package. Thanks for mentioning it. – Gonzalo Medina May 17 '11 at 19:09
Thanks, just what I was looking for! – Jamie Vicary May 17 '11 at 19:22
How about rounded corners for a little extra elegance? – Loop Space May 17 '11 at 20:42

You can add the [fill] parameter to any text node. This doesn't get you a halo around each character, but it might be more readable, depending on what your needs are.

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[fill,red] (0,0) rectangle (11,2);
\draw[very thick] (1,1) -- node[outer sep=5pt,fill=white,above] {Label} (10,1);
\end{tikzpicture}
\end{document}


-
Thanks Alan, it hadn't occurred to me that there was an easier way to solve the problem :) – Jamie Vicary May 17 '11 at 19:22

We can use \special{pdf: code ...} operator or \pdfliteral{} operator. Zdeněk Wagner, a Czech TeXist, mentions some technical details in the Zpravodaj journal 1/2013 of the Czechoslovak TeX Users Group.

I like a new toy named pdf-trans and those examples created by Paweł Jack­owski. I illustrate the use of the \boxgs command. I also like another preprepared commands, e.g. \boxshow and \boxmarkers, so I added them to that example even if they are not directly related to the Question.

The Tr operator is selecting the method of displaying a font (0, 1, or 2) with q operator which must be paired with Q operator to fit Adobe PDF specifications. It is not easy to describe these operators in detail. Then, we use w operator (dimension in big points) to set width of outline curves and the rest is easy. We set colors for font and its outline by rg&RG (rgb), k&K (cmyk) or g&G (gray). We usually use numbers before its operator, the PostScript users already know that.

I enclose an example where I set red font and sort of green outline, width is 3bp (it is very close to 3pt).

%! {pdf|lua}latex mal-outline.tex
\documentclass[a4paper]{article}
\usepackage{tikz}
\pagestyle{empty}\parindent=0pt
\input pdf-trans
\newbox\qbox
\begin{document}
\huge
\def\maltext{An example.}
\setbox\qbox=\hbox{\maltext}%
\begin{tikzpicture}
\node[align=center, draw]{%
\boxgs{Q q 2 Tr 0.3 w 0.47 0.6157 0.38 RG 1 0 0 rg }{}%
\copy\qbox \\
\boxshow{0.3 w 0 0 1 RG}{[1 1]0 d}{}
\copy\qbox \\
\boxmarkers{-12pt}{3pt}{0.3 w 1 0 0 RG 1 J}
\copy\qbox
};
\end{tikzpicture}
%\normalsize Text continues here.
\end{document}


-
The question is about tikz nodes. Does the code work in tikz? Otherwise, it isn't an option to the OP. – dustin Apr 4 '14 at 17:59
@dustin Good point, there could be a problem. I have updated the post. – Malipivo Apr 4 '14 at 18:13