Customize circled numbers

Edit after @marmot's (kind :P) comment (thanks):

This answer REALLY takes some care about a somehow constant size for all letters but not when line width is increased/decreased, because I had to set this parameter as extra or make some hacks in the way that it will be provided to my macro and I am not really sure if the OP need to change the line width (I just gave it initally as an idea about improving the appearance.)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\newlength{\mylength}
\xdef\CircleFactor{1.1}
\setlength\mylength{\dimexpr\f@size pt}
\newsavebox{\mybox}

\newcommand*\circled[2][draw=blue]{\savebox\mybox{\vbox{\vphantom{WL1/}#1}}\setlength\mylength{\dimexpr\CircleFactor\dimexpr\ht\mybox+\dp\mybox\relax\relax}\tikzset{mystyle/.style={circle,#1,minimum height={\mylength}}}
\tikz[baseline=(char.base)]
\node[mystyle] (char) {#2};}
\makeatother
\begin{document}
This answer takes care of the current font size in a way that \circled{i} and \circled{W} will appear the same size here and in the next paragpaph with a \verb|\tiny| (or whatever font).

\noindent Demonstration:\\
\circled{i}\circled{i}\circled{W}\circled{W}\\
\circled{W}\circled{W}\circled{i}\circled{i}

\tiny This answer takes care of the current font size in a way that \circled{i} and \circled{W} will appear the same size here and in the this with a \verb|\tiny| (or whatever font).

\noindent Demonstration:\\
\noindent\circled{i}\circled{i}\circled{W}\circled{W}\\
\circled{W}\circled{W}\circled{i}\circled{i}

\normalsize Also you can give a parameter inside an optional argument of the command and have different appearance:

\circled[text=blue,fill=red,draw=black]{W}\circled[fill=yellow,draw=red,text=blue]{W}

\end{document}

enter image description here

This is the old answer that DOESN'T REALY take care for the size according to my answer here.

You will select the style and could add your preferred style inside the optional argument of the definition of the command (I just placed the draw=blue option)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\def\mfontsize{\f@size}
\newcommand*\circled[2][draw=blue]{\tikzset{mystyle/.style={circle,#1,minimum height={\mfontsize*1.4}}}\tikz[baseline=(char.base)]
{
    \node[mystyle] (char) {\vphantom{WAH1g}#2};}}
\makeatother

\begin{document}
This is \circled{a} \circled[text=blue,fill=orange,draw=red, line width=0.3mm]{1} balloon.

\end{document}

enter image description here


You can add more parameters to the command.

MWE:

\documentclass[tikz,border=7pt]{standalone}
\newcommand*\circled[4]{\tikz[baseline=(char.base)]{
    \node[shape=circle, fill=#2, draw=#3, text=#4, inner sep=2pt] (char) {#1};}}

\begin{document}
    \circled{1}{red}{green}{blue}
\end{document}

Result:

enter image description here

You can also specify default values for the colors using \NewDocumentCommand from the xparse package:

\documentclass[tikz,border=7pt]{standalone}
\usepackage{xparse}
\NewDocumentCommand{\circleddefaults}{
    O{black}
    O{yellow}
    O{orange}
    m
    }{
    \tikz[baseline=(char.base)]{
    \node[shape=circle, fill=#1, draw=#2, text=#3, inner sep=2pt] (char) {#4};}}

\newcommand*\circled[4]{\tikz[baseline=(char.base)]{
    \node[shape=circle, fill=#2, draw=#3, text=#4, inner sep=2pt] (char) {#1};}}

\begin{document}
    \circled{1}{red}{green}{blue}

    \circleddefaults[blue,brown]{2} % two colors specified, third default

    \circleddefaults{3} % all defaults
\end{document}

enter image description here


Here is a "double" customization that defines :

  • an optional parameter of \circled that can be any style applied to the node,
  • an optional every number style that, if defined, is applied to all circled numbers.
\documentclass[border=7pt]{standalone}
\usepackage{tikz}
\newcommand*\circled[2][]{\tikz[baseline=(char.base)]{
    \node[shape=circle,draw,inner sep=2pt,#1,every number/.try] (char) {#2};}}
\tikzstyle{every number}=[draw=red] % I like to use tikzstyle ;)
\begin{document}
    \circled{1}\circled[fill=yellow]{2}
\end{document}

enter image description here

Tags:

Tikz Pgf