1

If I have a letter of type "(vertical) rectangle with rounded corners" and put it in a \colorbox{<fillcolor>}{text}, is there a way to set the colorbox-parameters so that exactly the inner area is filled with color?

Problem: The 'rounded corners' makes the task difficult. The setting \setlength{\fboxsep}{-0.57505pt} is probably not enough.

enter image description here

Hint 1: I created the "(vertical) rectangle with rounded corners" myself using FontForge.
For a test example, I used U+25A2 "White Square with Rounded Corners" (because I only found a square with rounded corners, not a rectangle) with the font Cambria.ttf.
The font is not important for the test example and can also be replaced, as long as the font contains U+25A2.

Hint 2: There is certainly something with TikZ, tcolorbox etc. But I try to find out whether one can do that with the normal \colorbox (!).

\documentclass[margin=5pt]{standalone}
\usepackage{xcolor}
\usepackage{fontspec}

\newfontfamily\myfont{Cambria}% a font that contains U+25A2
\newcommand\mychar{\myfont\char"25A2}%U+25A2 --->  White Square with Rounded Corners
\begin{document}
\setlength{\fboxsep}{-0.57505pt}
Test: \colorbox{yellow}{\mychar}
\end{document}
2
  • 2
    you can't do that. Either draw the curved box on the tex side (eg with tikz) and color there, or use a color font technology and color the font. Tex has no information about the glyph shape to color it. Commented Apr 19 at 13:06
  • I could imagine that this can be solved as a game with a few boxes, phantoms and possibly calculations.
    – cis
    Commented Apr 19 at 15:59

1 Answer 1

2

I don't know for your custom letter, but for the char U+25A2 (▢) it seems that its bounding box doesn't fit its shape (at least, with the Cambria font). I don't have the Cambria font installed, but it's the same with DejaVu Sans:

\documentclass[margin=5pt]{standalone}
\usepackage{xcolor}
\usepackage{fontspec}

\newfontfamily\myfont{DejaVu Sans}% a font that contains U+25A2
\newcommand\mychar{\myfont\char"25A2}%U+25A2 --->  White Square with Rounded Corners

\begin{document}

\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{0.05pt}
\fcolorbox{red}{white}{\mychar}

\end{document}

▢ bounding box

With a square, you can use the height to correct the horizontal bounding box, with something like

\sbox{0}{\mychar}%
\dimen0=\dimexpr \wd0 - \ht0 - \dp0\relax%
\setlength{\fboxsep}{-0.25\dimen0}%
\colorbox{yellow}{\kern -0.5\dimen0\usebox{0}\kern -0.5\dimen0}%

Maybe you could do something similar with your custom shape.

Full example, with char U+25A2 and DejaVu Sans font:

\documentclass[margin=5pt]{standalone}
\usepackage{xcolor}
\usepackage{fontspec}

\newfontfamily\myfont{DejaVu Sans}% a font that contains U+25A2
\newcommand\mychar{\myfont\char"25A2}%U+25A2 --->  White Square with Rounded Corners

\newcommand\myfilledchar{%
  \begingroup%
  \sbox{0}{\mychar}%
  \dimen0=\dimexpr \wd0 - \ht0 - \dp0\relax%
  \setlength{\fboxsep}{-0.25\dimen0}%
  \colorbox{yellow}{\kern -0.5\dimen0\usebox{0}\kern -0.5\dimen0}%
  \endgroup%
}

\begin{document}

\myfilledchar
\quad
{\Huge \myfilledchar}
\quad
{\tiny \myfilledchar}

\end{document}

Full example

1
  • 1
    Looks promising, I'll study it tomorrow. :)
    – cis
    Commented Apr 19 at 21:03

You must log in to answer this question.

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