Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}}

EDIT: 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

share|improve this question
add comment

4 Answers 4

up vote 8 down vote accepted
 \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)

share|improve this answer
    
David your solution is a very good and simple solution, but I want a smaller height. –  Kiyoshi Apr 27 at 9:35
    
@Kiyoshi replace \strut by \rule[1pt]{0pt}{5pt} replacing 1 and 5 for whatever depth and height you want –  David Carlisle Apr 27 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 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? –  David Carlisle Apr 27 at 10:20
    
Exactly, I want a individually centred. –  Kiyoshi Apr 27 at 10:30
show 2 more comments

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

share|improve this answer
    
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 at 10:00
    
@Kiyoshi They are already centered horizontally by default. –  Harish Kumar Apr 27 at 12:53
add comment

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 as 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

share|improve this answer
    
The problem with eqparbox is when I adjust the box to a character normal size the height is not equal. –  Kiyoshi Apr 27 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 at 10:55
    
@ Bernard: Same size and centred –  Kiyoshi Apr 27 at 10:58
    
@Kiyoshi: Updated my answer. I added two solutions to the height problem. –  Bernard Apr 27 at 12:14
add comment

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

share|improve this answer
    
Nice solution, but I want a smaller height box. –  Kiyoshi Apr 27 at 9:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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