20

my question is simple (or not) I want to set the size of the \colorbox command or \fcolorbox command for a single character. The problem is the next:

character box

As seen in the image, the height and width are different for different characters, but what I want is that the size is the same for all characters. The package that I use is xcolorand the code I used to generate the box character is \noindent\colorbox{black!85}{\textcolor{white}{i}}

My objective is a box with individually centred character. The background of the box in black color and text in white. Also would be nice if it is a enumitem and the items are in a black box

5 Answers 5

21
 \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)

6
  • David your solution is a very good and simple solution, but I want a smaller height.
    – Kiyoshi
    Apr 27, 2014 at 9:35
  • @Kiyoshi replace \strut by \rule[1pt]{0pt}{5pt} replacing 1 and 5 for whatever depth and height you want Apr 27, 2014 at 9:52
  • If possible to create a colorbox with the same width and height for all character but the characters in center of a box?
    – Kiyoshi
    Apr 27, 2014 at 10:17
  • @Kiyoshi makebox centres by default, so the characters should be horizontally centred, and by adjusting the height and depth you use in the strut you can make the baseline be wherever you want or do you want an A to be lower than an a so they are individually centred rather than being on the same baseline? Apr 27, 2014 at 10:20
  • Exactly, I want a individually centred.
    – Kiyoshi
    Apr 27, 2014 at 10:30
8

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}

enter image description here

2
  • I can control perfectly the width and the height, if is possible to center the character in a box that would be amazing ;)
    – Kiyoshi
    Apr 27, 2014 at 10:00
  • @Kiyoshi They are already centered horizontally by default.
    – user11232
    Apr 27, 2014 at 12:53
5

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} 

enter image description here

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 to eqmakebox. 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} 

enter image description here

4
  • The problem with eqparbox is when I adjust the box to a character normal size the height is not equal.
    – Kiyoshi
    Apr 27, 2014 at 9:46
  • @ Kiyoshi: Do you mean that if your letters have different sizes, hence differnt heights, the height of the coloured boxes will be different, and you'd like it to be the same?
    – Bernard
    Apr 27, 2014 at 10:55
  • @ Bernard: Same size and centred
    – Kiyoshi
    Apr 27, 2014 at 10:58
  • @Kiyoshi: Updated my answer. I added two solutions to the height problem.
    – Bernard
    Apr 27, 2014 at 12:14
3

The first optional argument is the textcolor and the second is the background color.

\documentclass{article}
\usepackage[x11names]{xcolor}
\makeatletter
\def\Cbox{\@ifnextchar[\Cbox@i{\Cbox@i[white]}}
\def\Cbox@i[#1]{\@ifnextchar[{\Cbox@ii[#1]}{\Cbox@ii[#1][black]}}
\def\Cbox@ii[#1][#2]#3{\colorbox{#2}{\makebox[1em]{\strut\textcolor{#1}{#3}}}}
\makeatother
\begin{document}

\Cbox{I} \Cbox[yellow]{g} \Cbox[AntiqueWhite1][DeepPink4]{W}

\end{document} 

enter image description here

1
  • Nice solution, but I want a smaller height box.
    – Kiyoshi
    Apr 27, 2014 at 9:52
2

Here a solution with the adjustbox package. You can create the centered box using the center=<width> key. Adding \strut gives you the same height and depths independent of the character size. If you use \ht\strutbox+\dp\strutbox as the width you will get a nice square.

\documentclass{article}
\usepackage{xcolor}
\usepackage{adjustbox}

\newcommand\blackboxchar[1]{\noindent
    \adjustbox{center=\ht\strutbox+\dp\strutbox,frame,bgcolor=black!85}{\color{white}\strut #1}}

\begin{document}

\begin{itemize}
\item[\blackboxchar{i}] text  text  text  text 

\item[\blackboxchar{a}] text  text  text  text 

\item[\blackboxchar{m}] text  text  text  text 
\end{itemize}

\end{document}

enter image description here

You must log in to answer this question.

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