Set size of \colorbox for a single character
\colorbox{black!85}{\makebox[2em]{\strut\textcolor{white}{a}}}
or any other width that you want.
Or as if indicated in comments you want vertical as well as horizontal centering
\colorbox{black!85}{\makebox(12,12){\textcolor{white}{a}}}
where the size of the box, 12, is given in picture environment units (pt
by default)
With tikz
:
\documentclass{article}
\usepackage{tikz}
\newcommand\coloredtext[2][]{\tikz[baseline=(char.base)]\node[minimum width=2em,text height=1.5ex, text depth=0.1ex,fill=black!85,text=white,#1](char){#2};}%
\begin{document}
\noindent
\foreach \x in {a,b,...,z}{%
\coloredtext{\x}\,
}
\noindent
\foreach \x in {A,B,...,Z}{%
\coloredtext[minimum width=2.55em]{\x}
}%
\end{document}
I would suggest using the eqparbox
package, if you want to have equal and minimal widths of the boxes (two runs are required):
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{eqparbox}
\usepackage[x11names]{xcolor}
\begin{document}
\colorbox{DeepPink4}{\eqmakebox[L]{\Large\bfseries\color{AntiqueWhite1}I}}
\colorbox{DeepPink4}{\eqmakebox[L]{\Large\bfseries\color{AntiqueWhite1}W}}
\end{document}
If you want the coloured boxes to have the same heights, while keeping the natural width of the widest, you can adjust the height in two ways:
- Either insert a
\vphantom{highest entry}
as an argument toeqmakebox
. Of course, it's up to you to decide which will be the highest entry. One may turn it into an optional argument of a macro. - Or insert an invisible rule that lets you predefine the height of the box.
I give an example of both, the second solution with predefined height equal to 1 cm. This can make possible fancy effects.
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{eqparbox}
\usepackage[x11names]{xcolor}
\begin{document}
\colorbox{DeepPink4}{\eqmakebox[L]{\Large\bfseries\color{AntiqueWhite1}I}}
\enspace
\colorbox{DeepPink4}{\eqmakebox[L]{\Large\bfseries\color{AntiqueWhite1}W}}
\enspace
\colorbox{DeepPink4}{\eqmakebox[L]{\vphantom{\Large\bfseries W}\footnotesize\bfseries\color{AntiqueWhite1}W}}
\enspace\\
\colorbox{AntiqueWhite1}{\eqmakebox[L]{\rule{0pt}{\dimexpr 1cm-2\fboxsep\relax}\Large\bfseries\color{DeepPink4}W}}
\enspace
\colorbox{AntiqueWhite1}{\eqmakebox[L]{\rule{0pt}{\dimexpr 1cm-2\fboxsep\relax}\footnotesize\bfseries\color{DeepPink4}i}}
\enspace\\
\colorbox{AntiqueWhite1}{\eqmakebox[L]{\rule[-0.4cm]{0pt}{\dimexpr 1cm-2\fboxsep\relax}\Large\bfseries\color{DeepPink4}W}}
\enspace
\colorbox{AntiqueWhite1}{\eqmakebox[L]{\rule[-0.4cm]{0pt}{\dimexpr 1cm-2\fboxsep\relax}\footnotesize\bfseries\color{DeepPink4}i}}
\end{document}