9

I am trying to generate this in my document:

Teletype

The goal is to have multiple lines that are aligned, which is why I used \texttt{}. But using \colorbox{color}{text} from the xcolor package, this is what I get (don't mind the different font or color):

Colorbox

So my questions are:

  1. How can I remove the spacing inside the colorboxes?
  2. Is there a more elegant way of doing this?

This is my MWE:

\documentclass[parskip]{scrbook} 
\usepackage{xcolor}
\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}
  • To remove the extra space just use \setlength{\fboxsep}{0pt}. – Phelype Oleinik Jul 4 '18 at 12:09
  • 2
    There is also the texshade package, see ctan.org/pkg/texshade and tug.org/texshowcase/texshade-eg.pdf for an example. – Marijn Jul 4 '18 at 12:14
  • @PhelypeOleinik - The problem with \setlength{\fboxsep}{0pt} is that it also removes the vertical space in the box, which I would like to keep. – basseur Jul 4 '18 at 12:32
  • @basseur Yes, I noticed it now. I'm doing something :) – Phelype Oleinik Jul 4 '18 at 12:33
  • 1
    You might also look at the pgfmolbio package. – Ross Jul 4 '18 at 12:53
10

It was fun :)

enter image description here

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.

  • 1
    +1 for you, very nice solution :) – GiuTeX Jul 4 '18 at 13:16
4

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

enter image description here

  • The problem with \setlength{\fboxsep}{0pt} is that it also removes the vertical space in the box, which I would like to keep. Is that possible? – basseur Jul 4 '18 at 12:32
  • 1
    You can add a \strut inside the box you want bigger in vertical – GiuTeX Jul 4 '18 at 12:59
4

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}

enter image description here

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}

enter image description here

  • 2
    You can replace the \rule[-.5ex]{0pt}{2.5ex} by a \strut and the size will be adjusted exactly. – Phelype Oleinik Jul 4 '18 at 13:09
  • 1
    yes, indeed, but with rule you can control spacing above and below, anyway to complete it is good to add the "strutted version" – GiuTeX Jul 4 '18 at 13:13
  • 1
    Sooorry, but I had to pull my "select-as-answer" for the very elegant answer from @PhelypeOleinik ;) – basseur Jul 4 '18 at 13:54
  • 1
    Don't worry, I see that his answer is much more robust, anyway I proposed my alternative as being more simple and maybe intuitive for dummies, but I guess I would have used the answer from @PhelypeOleinik too (if I knew it before) ;) – GiuTeX Jul 4 '18 at 14:43
4

You can have a syntax that allows to align the letters in the source:

  1. !c{...} denotes a colored block, where c is either b, g or r
  2. ~ means a “real space”
  3. spaces are ignored and can be used for aligning the characters in the source.

Here's the code:

\documentclass{article}
\usepackage{xcolor}

\newenvironment{basseur}
 {%
  \makeactives
  \fboxsep=0pt
  \raggedright
  \small\ttfamily
  \sbox0{A}%
  \edef\shortstrut{%
    \vrule height \the\dimexpr 1pt+\ht0\relax
           depth 1pt
           width 0pt
  }%
  \baselineskip=\dimexpr1.05\ht0+\dp0\relax
  \quotation
 }
 {\endquotation}

\newcommand{\makeactives}{%
  \begingroup\lccode`~=`!\lowercase{\endgroup\let~}\startcolor
  \catcode`!=\active
  \catcode`~=10
  \catcode` =9
}
\newcommand{\makeactive}[2]{%
}
\newcommand{\startcolor}[2]{%
  \colorbox{#1basseur}{\shortstrut\color{white}#2}%
}

\colorlet{rbasseur}{red}
\colorlet{bbasseur}{black}
\colorlet{gbasseur}{gray}

\begin{document}

\begin{basseur}
AjuC~ER7~(R)~(24)E!b{V R   AAGLNF}L!b{DVL}S!b{A}!g{M}!b{G M}!r{R}!b{P}DVEPG   G VPR!b{LGGECA}\\
AjuE~ER9~(R)~(24)R!b{V}E!b{AAGLNF R   DVL}V   S !b{L    G}G !r{R}   I D--QS!b{D}EII!b{LGGECA}
\end{basseur}

\end{document}

enter image description here

2

I went ahead and took opisthofulax' answer and enhanced it in my own way. I did not want to permanently change the \fboxsep for the whole document, so I added two switch commands. Maybe this will help someone in the future!

MWE:

\documentclass[parskip]{scrbook} 
\usepackage{xcolor}

\newlength{\oldfboxsep}

\newcommand{\switchfboxZero}{% Stores current \fboxsep and sets it to 0pt
    \setlength{\oldfboxsep}{\fboxsep}%
    \setlength{\fboxsep}{0pt}%
}
\newcommand{\switchfboxPrevious}{% Restores previous \fboxsep value
    \setlength{\fboxsep}{\oldfboxsep}%
}

\newcommand{\colorWB}[1]{%
    \switchfboxZero%
    \color{white}\colorbox{black}{\strut{#1}}\color{black}%
    \switchfboxPrevious%
}

\newcommand{\colorWG}[1]{%
    \switchfboxZero%
    \color{white}\colorbox{gray}{\strut{#1}}\color{black}%
    \switchfboxPrevious%
}
\newcommand{\colorWR}[1]{%
    \switchfboxZero%
    \color{white}\colorbox{red}{\strut{#1}}\color{black}%
    \switchfboxPrevious%
}
\newcommand{\colorBG}[1]{%
    \switchfboxZero%
    \color{black}\colorbox{green}{\strut{#1}}%
    \switchfboxPrevious%
}

\begin{document}
    \texttt{AjuC ER7 (R) (24) %
        E%
        \colorWB{VRAAGLNF}%
        L%
        \colorWB{DVL}%
        S%
        \colorWB{A}%
        \colorWG{M}%
        \colorWB{GM}%
        \colorWR{R}%
        \colorWB{P}%
        DVEPGGVPR%
        \colorWB{LGGECA}%
    }\\
    \texttt{AjuE ER9 (R) (24) %
        R%
        \colorWB{V}%
        E%
        \colorWB{AAGLNFRDVL}%
        VS%
        \colorWB{LG}%
        G%
        \colorWR{R}%
        ID--QS%
        \colorWB{D}%
        EII%
        \colorWB{LGGECA}%
    }
\end{document}
2

While the OP is asking for a solution in LaTeX I want to present also two solutions how the problem can be solved with ConTeXt.

The first method is to use the \framed mechanism where you can use the backgroundcolor and foregroundcolor keys to change the colors of the text and background.

\setupbodyfont[tt]

\defineframed
  [BackgroundColor]
  [frame=off,
   rulethickness=0pt,
   offset=0pt,
   background=color,
   foregroundcolor=white]

\defineframed [BackgroundR] [BackgroundColor] [backgroundcolor=red]
\defineframed [BackgroundK] [BackgroundColor] [backgroundcolor=black]
\defineframed [BackgroundS] [BackgroundColor] [backgroundcolor=gray]

\starttext

AjuC ER7 (R) (24)
E%
\BackgroundK{VRAAGLNF}%
L%
\BackgroundK{DVL}%
S%
\BackgroundK{A}%
\BackgroundS{M}%
\BackgroundK{GM}%
\BackgroundR{R}%
\BackgroundK{P}%
DVEPGGVPR%
\BackgroundK{LGGECA}

AjuE ER9 (R) (24)
R%
\BackgroundK{V}%
E%
\BackgroundK{AAGLNFRDVL}%
VS%
\BackgroundK{LG}%
G%
\BackgroundR{R}%
ID--QS%
\BackgroundK{D}%
EII%
\BackgroundK{LGGECA}

\stoptext

Adding background color to text

Another solution is the use of the processor mechanism which is normally used to have more control about the layout of register entries or the formatting of the section counter.

\setupbodyfont[tt]

\defineprocessor [BackgroundColor] [color=white]

% \define[2]\BackgroundColorLine
%   {\backgroundline[#1]{\strut#2}}
%
% \defineprocessor [BackgroundR] [BackgroundColor] [command=\BackgroundColorLine  {red}]
% \defineprocessor [BackgroundK] [BackgroundColor] [command=\BackgroundColorLine{black}]
% \defineprocessor [BackgroundS] [BackgroundColor] [command=\BackgroundColorLine {gray}]

\defineprocessor [BackgroundR] [BackgroundColor] [command=\groupedcommand{\backgroundline  [red]\bgroup\begstrut}{\endstrut\egroup}]
\defineprocessor [BackgroundK] [BackgroundColor] [command=\groupedcommand{\backgroundline[black]\bgroup\begstrut}{\endstrut\egroup}]
\defineprocessor [BackgroundS] [BackgroundColor] [command=\groupedcommand{\backgroundline [gray]\bgroup\begstrut}{\endstrut\egroup}]

\starttext

AjuC ER7 (R) (24)
E%
\applyprocessor{BackgroundK}{VRAAGLNF}%
L%
\applyprocessor{BackgroundK}{DVL}%
S%
\applyprocessor{BackgroundK}{A}%
\applyprocessor{BackgroundS}{M}%
\applyprocessor{BackgroundK}{GM}%
\applyprocessor{BackgroundR}{R}%
\applyprocessor{BackgroundK}{P}%
DVEPGGVPR%
\applyprocessor{BackgroundK}{LGGECA}

AjuE ER9 (R) (24)
R%
\applyprocessor{BackgroundK}{V}%
E%
\applyprocessor{BackgroundK}{AAGLNFRDVL}%
VS%
\applyprocessor{BackgroundK}{LG}%
G%
\applyprocessor{BackgroundR}{R}%
ID--QS%
\applyprocessor{BackgroundK}{D}%
EII%
\applyprocessor{BackgroundK}{LGGECA}

\stoptext

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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