15

In order to improve readability of some texts, I'd like to put a kind of "eyeliner" around my letters (actually, that's how memes manage to get readable texts on any picture).

Here is an exemple of what I have:

enter image description here

And what I want:

enter image description here

You can use if needed some lua code, as I'll use it with lualatex (and custom fonts).

Thank you!

MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz\node[fill=red,circle]{\color{white}\textbf{Hello} world};
\end{document}

--EDIT--

As stated in the comments, the contour package works great, except with custom fonts. Any idea for this font (compile with lualatex and put the font .ttf file in the font/ folder):

\documentclass{article}
% Special font
\usepackage{fontspec}
\setmainfont[Path=fonts/]{Heartbeat_in_Christmas.ttf}

\usepackage{tikz}
\usepackage[outline]{contour}
\begin{document}
\tikz\node[fill=red,circle]{\contour{black}{\protect\color{white}\textbf{Hello} world}};
\end{document}

-- EDIT 2 --

I found the solution when I use lualatex: I just need to use \usepackage{contour} instead of \usepackage[outline]{contour}!

-- EDIT 3 -- The solution of using with pdfrender + tikz to superpose texts works also great, except for a few chars with sharp angles:

enter image description here

Here is the text:

    \begin{tikzpicture} \node at (0,0) {%
        \textpdfrender{
          TextRenderingMode=FillStrokeClip,
          LineWidth=.8mm,
          FillColor=white,
          StrokeColor=black,
        }{mmmnnn}%
      };
      \node at (0,0) {mmmnnn};
    \end{tikzpicture}

-- EDIT 5 -- Adding MiterLimit=1 avoid these strange shark edges, now it's better, there is just a minor hole in the letters m and n, in my font, but it's not important:

enter image description here

Thank you!

tobiasBora
  • 8,684
  • 1
    contour package can do this see for example https://tex.stackexchange.com/questions/78386/outlined-characters-in-beamer – percusse Nov 08 '17 at 12:48
  • Please have a look at this question. – AboAmmar Nov 08 '17 at 12:55
  • @percusse It works great on the default font, but if I try a custom font, it fails. Cf. my updated question. – tobiasBora Nov 08 '17 at 13:08
  • @AboAmmar same comment as percusse, it works great on the default font, but if I try a custom font, it fails. Cf. my updated question. – tobiasBora Nov 08 '17 at 13:09
  • fails in which sense? The outlining is not done at the font level. It is overprinted 16 or more times so it should work. – percusse Nov 08 '17 at 16:54
  • @percusse it does not fail during compilation, but I can't see any contour when I display it. – tobiasBora Nov 08 '17 at 17:20
  • @percusse : Oh, I solved my problem, I needed to remove the outline option of contour, which makes sense. Sorry for that! – tobiasBora Nov 08 '17 at 17:53
  • 2
    I would recommend to use pdfrender instead of contour https://tex.stackexchange.com/a/387350/53795 pdfrender uses the ‘real’ font outline as possible per PDF spec, while contour just tries to fake it. – timothymctim Nov 08 '17 at 23:59
  • @timothymctim Thanks for the tip! The only problem of pdfrender is that the line is drawn on top the text instead of behind. If the line is very thin, it's ok, but if it's quite large, the render is quite ugly, and the text can even disapear behind the stroke... – tobiasBora Nov 09 '17 at 16:32
  • @timothymctim : I found a solution for the superposition : create a tikz picture, position two nodes at (0,0), with the first note containing the pdfrender text, and the second node only the text. It's just too bad, because in the font I'm using a few char have problems with sharp angles (see the edited answer). – tobiasBora Nov 09 '17 at 16:45
  • @timothymctim : Ok I found the solution: I need to put MitterLimit=1 and the result is way better. – tobiasBora Nov 09 '17 at 17:00
  • Could you write an answer with your solution (for everyone)? – Bobyandbob Nov 10 '17 at 10:21
  • @Bobyandbob : I guess that I should let percusse write it no? – tobiasBora Nov 10 '17 at 12:59
  • ... or @percusse . – Bobyandbob Nov 10 '17 at 13:19

1 Answers1

19

With the help of people in the comments, I finally came out with several solutions depending on what I need:

If you don't use lualatex and your font is supported (efficient solution)

Just use \usepackage[outline]{contour} and then \contour{green}{yourtext}

If you use lualatex, you can do (inneficient version):

Just use \usepackage{contour} and then \contour{green}{yourtext}. However, it is not really efficient because it will create lot's of copies. enter image description here

If you use lualatex and you want a thin border (efficient solution)

Then use the package \usepackage{pdfrender}, and use it this way:

\textpdfrender{
          TextRenderingMode=FillStrokeClip,
          LineWidth=.1pt,
          FillColor=white,
          StrokeColor=black,
          MiterLimit=1
        }{Your text}

However, if the stroke line is too big, then it will hide the text. Also, I don't know why, but if you do that, tikz picture does not longer work... Why???

enter image description here

If you use lualatex and you want a thick border and your text is short ("efficient" solution)

Then, the only solution I found is to draw twice the text, and use a tikzpicture to put them on top of the other one. The problem of this solution is that

\begin{tikzpicture}[anchor=base, baseline] \node at (0,0) {%
    \textpdfrender{
      TextRenderingMode=FillStrokeClip,
      LineWidth=1pt,
      FillColor=white,
      StrokeColor=black,
      MiterLimit=1
    }{Your text}%
  };
  \node at (0,0) {Your text};
\end{tikzpicture}

However, here it does not break the line in a paragraph, if you have a solution for that, please let me know. The inneficient solution 2 solve this problem.

enter image description here

All the solutions in one file:

\documentclass{article}
% Fonts
% \usepackage{fontspec}
% \setmainfont[Path=fonts/]{Heartbeat_in_Christmas.ttf}

\usepackage{tikz} \usetikzlibrary{positioning} % \usepackage[outline]{contour} % Sol 1 \usepackage{pdfrender} % Sol 2 & 3 \usepackage{contour} % Sol 4 \begin{document} % \contour{green}{\color{blue}\textbf{Hello} world, I like it.}

Solution 1: Does not work with lualatex, only pdftex.

Solution 2: Not really efficient \contour{green}{\color{blue}Your text}

Solution 3: (thin stroke)% \textpdfrender{ TextRenderingMode=FillStrokeClip, LineWidth=.1pt, FillColor=blue, StrokeColor=green, MiterLimit=1 }{Your text}% \textpdfrender{ TextRenderingMode=FillStrokeClip, LineWidth=.5pt, FillColor=blue, StrokeColor=green, MiterLimit=1 }{Your text}

Solution 4: On single line texts (comment solution 3 to see it, no idea why...)% \begin{tikzpicture}[anchor=base, baseline] \node at (0,0) {\textpdfrender{ TextRenderingMode=FillStrokeClip, LineWidth=1pt, FillColor=blue, StrokeColor=green, MiterLimit=1 }{Your text}}; \node at (0,0) {\color{blue}Your text}; \end{tikzpicture}

\end{document}

tobiasBora
  • 8,684