62

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.

0

3 Answers 3

85

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}

text with halo in tikz node

4
  • 9
    I didn't know the contour package. Thanks for mentioning it. Commented May 17, 2011 at 19:09
  • 2
    How about rounded corners for a little extra elegance? Commented May 17, 2011 at 20:42
  • 3
    It's worth noting that contour does not (yet?) work with XeTeX :( Commented Dec 24, 2016 at 6:10
  • Any idea on how to get this to work with multiple lines of text where the white border is tight around the text? Commented Jul 27, 2019 at 4:32
18

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}

output of code

1
  • Thanks Alan, it hadn't occurred to me that there was an easier way to solve the problem :) Commented May 17, 2011 at 19:22
8

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}

mwe

3
  • The question is about tikz nodes. Does the code work in tikz? Otherwise, it isn't an option to the OP.
    – dustin
    Commented Apr 4, 2014 at 17:59
  • 1
    @dustin Good point, there could be a problem. I have updated the post.
    – Malipivo
    Commented Apr 4, 2014 at 18:13
  • Somebody asked a question, tex.stackexchange.com/questions/313115/draw-border-of-a-line, to which I think this answer might apply. But I don't quite know how to master your \pdfliteral technique. Commented Jun 5, 2016 at 1:43

You must log in to answer this question.

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