4

When I'm adding a contour to my coloured maths I get a shifted halo when a \textcolor{} command is encountered. How can this be fixed?

\documentclass[border=2pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage[outline]{contour}\contourlength{1pt}
\newcommand{\border}[1]{\contour{red}{#1}}

\begin{document} \border{(F_{!!\infty})} \border{(\textcolor{white}{F}_{!!\infty})} \end{document}

enter image description here

Atcold
  • 1,727

2 Answers2

7

You need to avoid having any {} group or color restore after the F otherwise you lose the font metric information on subscript position.

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage[outline]{contour}\contourlength{1pt}
\newcommand{\border}[1]{\contour{red}{#1}}

\begin{document} \border{(F_{!!\infty})} \border{(\begingroup\color{white}F_{!!\begingroup\color{black}\infty\endgroup}\endgroup)} \end{document}

David Carlisle
  • 757,742
  • Thank you for explaining what's going on! Nevertheless, I've accepted Ulrike's answer because it provides a patch that fixes the behaviour of \textcolor{}{} throughout my entire document. – Atcold Apr 11 '22 at 16:30
7

With a current latex-dev (What is "latex-dev"?) you can use \mathcolor. (I also use $ $ for the math, that is less fragile). \mathcolor doesn't work yet with xcolor, but will soon:

\documentclass[border=2pt]{standalone}
\usepackage[dvipsnames]{color}
\usepackage[outline]{contour}\contourlength{1pt}
\newcommand{\border}[1]{\contour{red}{#1}}

\begin{document} \border{(F_{!!\infty})} \border{$\mathcolor{white}{F}{!!\infty}$} \border{$\textcolor{white}{F}{!!\infty}$}

\end{document}

enter image description here

Update

contour disables colors locally and this confused \mathcolor. I think this here would be better:

\documentclass[border=2pt]{standalone}
\usepackage{color}
\usepackage[outline]{contour}\contourlength{1pt}
\newcommand{\border}[1]{\contour{red}{#1}}
\makeatletter
\def\con@coloroff{%
   \def\set@color{%
      \c@lor@special\m@ne
        {color push \current@color}\aftergroup\reset@color}%
   \def\reset@color{\c@lor@special\m@ne{color pop}}}
\makeatother
\begin{document}
\border{\(F_{\!\!\infty}\)}
\border{\(\mathcolor{white}{F}_{\!\!\infty}\)}
\border{$\mathcolor{white}{F}_{\!\!\infty}$}

\end{document}

Ulrike Fischer
  • 327,261
  • Your update fixes the behaviour of textcolor from xcolor and it does not require mathcolor and therefore a conflict with xcolor and a compilation with pdflatex-dev (which may or may not be available to the user). I'd recommend your update as the main answer to my question and mathcolor and latex-dev as addendum. Thanks a lot! – Atcold Apr 11 '22 at 16:03
  • Shall I create a PR for xcolor / color packages with your answer? It seems a bug on their side. – Atcold Apr 11 '22 at 16:06
  • 1
    I don't see a change of textcolor in a current texlive or miktex with my code, the shift is still there. Imho you really need the \mathcolor - it was actually implemented it just for such cases. And no, the change to \con@coloroff is not a xcolor or color problem, this is something contour should change. – Ulrike Fischer Apr 11 '22 at 17:03
  • I'm compiling my document with the latest pdflatex (pdfTeX 3.141592653-2.6-1.40.23 (TeX Live 2022/dev)), your patch, and \textcolor{}{} from xcolor. I observe no shift whatsoever. – Atcold Apr 11 '22 at 17:11