5

I'd like to put bicolored text. This is what I did

\newlength\myl
\newlength\myh
\newcommand{\bicolor}[1]{
\setlength{\myl}{0pt-\widthof{#1}}%
\setlength{\myh}{\heightof{#1}}%
\raisebox{0.5\myh}{\color{red}\clipbox{0pt  {0.5\myh} 0pt 0pt}{\mbox{#1}}}%
\hspace{\myl}%
{\color{blue}\clipbox{0pt  0pt 0pt {0.5\myh}}{\mbox{#1}}}%
}

and the \bicolor{BiKolor stuff}

ok

kind of works... (some problems cutting at the end)

However, this does not work \bicolor{BiKolor$_2$}

enter image description here

What is a better way to proceed?

2 Answers 2

4

As already pointed out in this answer, the problem is that the depth is not taken into account. This answer also fixes the problem, but in a slightly different way than Steven: the height of the dividing line does not depend on the depth. Whether or not this is a good thing I do not know. Another difference is that spurious spaces get avoided (but this is not really essential).

\documentclass{article}
\usepackage{trimclip}
\usepackage{xcolor}
\newlength\myl
\newlength\myh
\newcommand{\bicolor}[1]{%
\setbox0\hbox{#1}%
\makebox[\wd0][c]{\raisebox{\dimexpr0.5\ht0-0.5\dp0}{\textcolor{red}{\clipbox{-1pt {\dimexpr0.5\ht0+0.5\dp0} -1pt -1pt}{\copy0}}}%
\hspace{\dimexpr-\wd0-2pt}%
{\textcolor{blue}{\clipbox{-1pt -1pt -1pt {0.5\ht0}}{\copy0}}}}%
}
\begin{document}
B\bicolor{BiKolor$_2$}B\bicolor{BiKolor stuff}\par
B{BiKolor$_2$}B{BiKolor stuff}
\end{document}

enter image description here

7

You need to account for the depth of the text below the baseline.

\documentclass{article}
\usepackage{xcolor,calc,trimclip}
\begin{document}
\newlength\myl
\newlength\myh
\newlength\myd
\newcommand{\bicolor}[1]{
\setlength{\myl}{0pt-\widthof{#1}}%
\setlength{\myh}{\heightof{#1}}%
\setlength{\myd}{\depthof{#1}}%
\raisebox{0.5\myh}{\color{red}\clipbox{0pt  {0.5\myh+\myd} 0pt 0pt}{\mbox{#1}}}%
\hspace{\myl}%
{\color{blue}\clipbox{0pt  0pt 0pt {0.5\myh}}{\mbox{#1}}}%
}

\bicolor{BiKolor stuff}

\bicolor{BiKolor$_2$}
\end{document}

enter image description here

Note that you lose a little bit of the "ff" font glyphs from clipping, because of font protrusion or "overshoot" (see What is the local height of a capital letter?). You can clip to -1pt on those boundaries to compensate, if desired.

However, if you clip -1pt on the left and right borders, it will make the result 2pt wider, so you need to \hspace leftwards 1pt at the start and end of the process. In the MWE, I show with and without bi-color to compare results.

\documentclass{article}
\usepackage{xcolor,calc,trimclip}
\newlength\myl
\newlength\myh
\newlength\myd
\newcommand{\bicolor}[1]{\hspace{-1pt}%
\setlength{\myl}{0pt-\widthof{#1}}%
\setlength{\myh}{\heightof{#1}}%
\setlength{\myd}{\depthof{#1}}%
\raisebox{0.5\myh}{\color{red}\clipbox{-1pt  {0.5\myh+\myd} -1pt -1pt}%
  {\mbox{#1}}}%
\hspace{\myl-2pt}%
{\color{blue}\clipbox{-1pt  -1pt -1pt {0.5\myh}}{\mbox{#1}}}%
\hspace{-1pt}}

\begin{document}
BiKolor stuff

\bicolor{BiKolor stuff}

\bicolor{BiKolor$_2$ Xg jRETY}

BiKolor$_2$ Xg jRETY
\end{document}

enter image description here

2
  • Even if you use \newcommand{\bicolor}[1]{% instead if \newcommand{\bicolor}[1]{ the command still inserts a bit of spurious horizontal space, probably because of your overshoot compensation.
    – user240002
    Commented Apr 17, 2021 at 22:56
  • @user240002 It does make it 2pt wider than before. The way to compensate is to kern left 1pt at the beginning and at the end of the process. Perhaps I'll revise my answer. Commented Apr 17, 2021 at 23:26

You must log in to answer this question.

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