4

I need to encircle letters. I am aware of \textcircled{$x$}. However I want to be able to fill the circle with a colour, and also change the colour of text.

Specifically, I am looking for something like \ding{182} to \ding{188} in pifont (documentation available here, page 47, Table 171).

I want to be able to do this without TikZ. Will greatly appreciate some suggestions and help.

  • 1
    Welcome to TeX.SE! Also without tcolorbox? – CarLaTeX Nov 20 '18 at 8:16
  • What about pstricks instead of tikz? What's your reason for wanting to avoid tikz? – David Purton Nov 20 '18 at 12:53
8

Here's a solution using the picture environment. Update to support font sizes up to \large. The filled circles don't work with larger font sizes than this. You could work around this limitation with \usepackage{pict2e}.

You could make the interface nicer by allowing for default values to be set and optional key values. This could be done using l3keys (or other means) but since you don't want tikz perhaps you are aiming for minimal dependencies.

(Hoping for a vote from @DavidCarlisle for choosing to go with the picture environment. Although I'm sure I lack his skill with it.)

MWE

You might want to experiment to put the circles at the vertical position you want.

This code centres them on the \strut height:

\setlength{\dcircle}{\dp\strutbox}%
\addtolength{\dcircle}{\ht\strutbox}%
\setlength{\rcircle}{0.5\dcircle}%
\setlength{\dpcircle}{\rcircle}%
\addtolength{\dpcircle}{-\dp\strutbox}%

The code used in this full example centres them on numbers. Whichever you take, some characters will look odd. You could centre them on the character in being circled, but this will look a little odd if you have circles next to each other at different positions.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{xcolor}
\newlength{\dpcircle}
\newlength{\rcircle}
\newlength{\dcircle}
% \docircle{fill}{draw}{textcolour}{text}
\newcommand{\docircle}[4]{%
  \setlength{\dpcircle}{\dp\strutbox}%
  \setlength{\dcircle}{\dpcircle}%
  \addtolength{\dcircle}{\ht\strutbox}%
  \setlength{\rcircle}{0.5\dcircle}%
  \setlength{\unitlength}{1sp}%
  \begin{picture}(\number\dcircle,0)
    \color{#1}
    \put(\number\rcircle,\number\dpcircle){\circle*{\number\dcircle}}
    \color{#2}
    \put(\number\rcircle,\number\dpcircle){\circle{\number\dcircle}}
    \put(\number\rcircle,0){\makebox[0pt]{\textcolor{#3}{#4}}}
  \end{picture}%
}
\begin{document}
Numbers:
\docircle{yellow}{red}{blue}{0}
\docircle{yellow}{red}{blue}{1}
\docircle{yellow}{red}{blue}{2}
\docircle{yellow}{red}{blue}{3}
\docircle{yellow}{red}{blue}{4}
\docircle{yellow}{red}{blue}{5}
\docircle{yellow}{red}{blue}{6}
\docircle{yellow}{red}{blue}{7}
\docircle{yellow}{red}{blue}{8}
\docircle{yellow}{red}{blue}{9}

Lower-case:
\docircle{green}{magenta}{blue}{a}
\docircle{green}{magenta}{blue}{b}
\docircle{green}{magenta}{blue}{c}
\docircle{green}{magenta}{blue}{d}
\docircle{green}{magenta}{blue}{e}
\docircle{green}{magenta}{blue}{f}
\docircle{green}{magenta}{blue}{g}
\docircle{green}{magenta}{blue}{h}
\docircle{green}{magenta}{blue}{i}
\docircle{green}{magenta}{blue}{j}
\docircle{green}{magenta}{blue}{k}
\docircle{green}{magenta}{blue}{l}
\docircle{green}{magenta}{blue}{m}
\docircle{green}{magenta}{blue}{n}
\docircle{green}{magenta}{blue}{o}
\docircle{green}{magenta}{blue}{p}
\docircle{green}{magenta}{blue}{q}
\docircle{green}{magenta}{blue}{r}
\docircle{green}{magenta}{blue}{s}
\docircle{green}{magenta}{blue}{t}
\docircle{green}{magenta}{blue}{u}
\docircle{green}{magenta}{blue}{v}
\docircle{green}{magenta}{blue}{w}
\docircle{green}{magenta}{blue}{x}
\docircle{green}{magenta}{blue}{y}
\docircle{green}{magenta}{blue}{z}

Upper-case:
\docircle{cyan}{black}{white}{A}
\docircle{cyan}{black}{white}{B}
\docircle{cyan}{black}{white}{C}
\docircle{cyan}{black}{white}{D}
\docircle{cyan}{black}{white}{E}
\docircle{cyan}{black}{white}{F}
\docircle{cyan}{black}{white}{G}
\docircle{cyan}{black}{white}{H}
\docircle{cyan}{black}{white}{I}
\docircle{cyan}{black}{white}{J}
\docircle{cyan}{black}{white}{K}
\docircle{cyan}{black}{white}{L}
\docircle{cyan}{black}{white}{M}
\docircle{cyan}{black}{white}{N}
\docircle{cyan}{black}{white}{O}
\docircle{cyan}{black}{white}{P}
\docircle{cyan}{black}{white}{Q}
\docircle{cyan}{black}{white}{R}
\docircle{cyan}{black}{white}{S}
\docircle{cyan}{black}{white}{T}
\docircle{cyan}{black}{white}{U}
\docircle{cyan}{black}{white}{V}
\docircle{cyan}{black}{white}{W}
\docircle{cyan}{black}{white}{X}
\docircle{cyan}{black}{white}{Y}
\docircle{cyan}{black}{white}{Z}

Text sizes (up to \verb|\large|):
\tiny \docircle{magenta}{green}{black}{0}
\scriptsize \docircle{magenta}{green}{black}{0}
\footnotesize \docircle{magenta}{green}{black}{0}
\small \docircle{magenta}{green}{black}{0}
\normalsize \docircle{magenta}{green}{black}{0}
\large \docircle{magenta}{green}{black}{0}
\end{document}

output

  • +1 \number\dimexpr.5\fontcharht\font`#4\relax gives amusing result (in place of \number\dpcircle) (but this is what you allude too already) – user4686 Nov 21 '18 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.