Using background colors in monospaced text
It was fun :)
You asked two questions, so there are two answers:
The first one is the box. I copied the definition of xcolor
's \colorbox
, modified it, and called it \tallcbox
. The difference is that the size of the \colorbox
is the size of its contents plus \fboxsep
to each side.
I changed \tallcbox
to have the same width as the contents, and have height and depth equal to the height and depth of a \strut
.
The second is the "more elegant way of doing this". Elegance is subjective, so I presumed that something along the lines of "less code" and "less" is what you want :)
I defined another command, \letterbox
, which take the same arguments as \colorbox
or \tallcbox
. But \letterbox
first converts the given color to grayscale using \colorlet{temp@c}[gray]{#1}
. Then it uses \extractcolorspec{temp@c}{\color@spec}
to get the properties of this converted color into \color@spec
. The \color@spec
for a gray color will be {gray}{<0--1>}
, where <0--1>
denotes a number, you guesses, between 0 and 1. Finally, \letterbox
compares this number with 0.5
and sets the text color to black or white, depending on the value of <0--1>
.
Here's the code:
\documentclass[parskip]{scrbook}
\usepackage{xcolor}
\makeatletter
\def\black@or@white#1#2{%
\@tempdima#2 pt
\ifdim\@tempdima>0.5 pt
\definecolor{temp@c}{gray}{0}%
\else
\definecolor{temp@c}{gray}{1}%
\fi}
\def\letterbox#1#{\protect\letterb@x{#1}}
\def\letterb@x#1#2#3{%
\colorlet{temp@c}[gray]{#2}%
\extractcolorspec{temp@c}{\color@spec}%
\expandafter\black@or@white\color@spec
{\color#1{temp@c}\tallcbox#1{#2}{#3}}}
\def\tallcbox#1#{\protect\color@box{#1}}
\def\color@box#1#2{\color@b@x\relax{\color#1{#2}}}
\long\def\color@b@x#1#2#3%
{\leavevmode
\setbox\z@\hbox{{\set@color#3}}%
\ht\z@\ht\strutbox
\dp\z@\dp\strutbox
{#1{#2\color@block{\wd\z@}{\ht\z@}{\dp\z@}\box\z@}}}
\makeatother
\begin{document}
\texttt{AjuC ER7 (R) (24)%
E%
\letterbox{black}{VRAAGLNF}%
L%
\letterbox{black}{DVL}%
S%
\letterbox{black}{A}%
\letterbox{gray}{M}%
\letterbox{black}{GM}%
\letterbox{red}{R}%
\letterbox{black}{P}%
DVEPGGVPR%
\letterbox{black}{LGGECA}%
}\\
\texttt{AjuE ER9 (R) (24)%
R%
\letterbox{black}{V}%
E%
\letterbox{black}{AAGLNFRDVL}%
VS%
\letterbox{black}{LG}%
G%
\letterbox{red}{R}%
ID--QS%
\letterbox{black}{D}%
EII%
\letterbox{black}{LGGECA}%
}
\end{document}
Ooh, and as the other answer says, you have to protect the end of the lines with %
to kill the extra spaces.
Use comment at end of line %
and give \setlength{\fboxsep}{0pt}
\documentclass[parskip]{scrbook}
\usepackage{xcolor}
\setlength{\fboxsep}{0pt}
\begin{document}
\texttt{AjuC ER7 (R) (24)
E%
\color{white}\colorbox{black}{VRAAGLNF}%
\color{black}L%
\color{white}\colorbox{black}{DVL}%
\color{black}S%
\color{white}\colorbox{black}{A}%
\colorbox{gray}{M}%
\colorbox{black}{GM}%
\colorbox{red}{R}%
\color{white}\colorbox{black}{P}%
\color{black}DVEPGGVPR%
\color{white}\colorbox{black}{LGGECA}
}\\
\texttt{AjuE ER9 (R) (24)
R%
\color{white}\colorbox{black}{V}%
\color{black}E%
\color{white}\colorbox{black}{AAGLNFRDVL}%
\color{black}VS%
\color{white}\colorbox{black}{LG}%
\color{black}G%
\color{white}\colorbox{red}{R}%
\color{black}ID--QS%
\color{white}\colorbox{black}{D}%
\color{black}EII%
\color{white}\colorbox{black}{LGGECA}
}
\end{document}
Here's the output
I'm almost re-taking the answer by @Mu30 murugans2katgmail and adding the vertical padding with the strut
trick (here created with an invisible rule):
\documentclass[parskip]{scrbook}
\usepackage{xcolor}
\setlength{\fboxsep}{0pt}
\let\oldcolbox\colorbox
\renewcommand\colorbox[2]{%
\oldcolbox{#1}{\rule[-.5ex]{0pt}{2.5ex}#2}%
}%
\begin{document}
\texttt{AjuC ER7 (R) (24) E\color{white}\colorbox{black}{VRAAGLNF}\color{black}L\color{white}\colorbox{black}{DVL}\color{black}S\color{white}\colorbox{black}{A}\colorbox{gray}{M}\colorbox{black}{GM}\colorbox{red}{R}\color{white}\colorbox{black}{P}\color{black}DVEPGGVPR\color{white}\colorbox{black}{LGGECA}
}\\
\texttt{AjuE ER9 (R) (24)
R\color{white}\colorbox{black}{\strut V}\color{black}E\color{white}\colorbox{black}{AAGLNFRDVL}\color{black}VS\color{white}\colorbox{black}{LG}\color{black}G\color{white}\colorbox{red}{R}\color{black}ID--QS\color{white}\colorbox{black}{D}\color{black}EII\color{white}\colorbox{black}{LGGECA}
}
\end{document}
You can control spacing above and below the letter by changing the value of the \rule
.
EDIT: as suggested if you want exactly the vertical padding to be centered just substitute the \rule
with a \strut
:
\documentclass[parskip]{scrbook}
\usepackage{xcolor}
\setlength{\fboxsep}{0pt}
\let\oldcolbox\colorbox
\renewcommand\colorbox[2]{%
\oldcolbox{#1}{\strut#2}%
}%
\begin{document}
\texttt{AjuC ER7 (R) (24) E\color{white}\colorbox{black}{VRAAGLNF}\color{black}L\color{white}\colorbox{black}{DVL}\color{black}S\color{white}\colorbox{black}{A}\colorbox{gray}{M}\colorbox{black}{GM}\colorbox{red}{R}\color{white}\colorbox{black}{P}\color{black}DVEPGGVPR\color{white}\colorbox{black}{LGGECA}
}\\
\texttt{AjuE ER9 (R) (24)
R\color{white}\colorbox{black}{\strut V}\color{black}E\color{white}\colorbox{black}{AAGLNFRDVL}\color{black}VS\color{white}\colorbox{black}{LG}\color{black}G\color{white}\colorbox{red}{R}\color{black}ID--QS\color{white}\colorbox{black}{D}\color{black}EII\color{white}\colorbox{black}{LGGECA}
}
\end{document}