在等宽文本中使用背景颜色

在等宽文本中使用背景颜色

我正在尝试在我的文档中生成这个:

电传打字机

目标是让多条线对齐,这就是我使用的原因\texttt{}。但是\colorbox{color}{text}使用xcolor包,这就是我得到的(不要介意不同的字体或颜色):

彩盒

我的问题是:

  1. 我怎样才能消除颜色框内的间距?
  2. 有没有更优雅的方法来做到这一点?

这是我的 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}

答案1

好玩 :)

在此处输入图片描述

你问了两个问题,所以有两个答案:

第一个是盒子。我复制了 的定义xcolor\colorbox修改了它,并将其命名为\tallcbox。不同之处在于 的大小\colorbox是其内容的大小加上\fboxsep每条边的大小。

我将其改为\tallcbox具有与内容相同的宽度,并且具有与的高度和深度相等的高度和深度\strut

第二个是“更优雅的方式”。优雅是主观的,所以我推测你想要的是“更少的代码”和“更少”之类的东西:)

我定义了另一个命令,它使用与或\letterbox相同的参数。但首先使用将给定的颜色转换为灰度。然后它使用将转换后的颜色的属性获取到。灰色的将是,其中表示一个介于 0 和 1 之间的数字(您猜)。最后,将此数字与进行比较,并根据的值将文本颜色设置为黑色或白色。\colorbox\tallcbox\letterbox\colorlet{temp@c}[gray]{#1}\extractcolorspec{temp@c}{\color@spec}\color@spec\color@spec{gray}{<0--1>}<0--1>\letterbox0.5<0--1>

代码如下:

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

哦,正如另一个答案所说,您必须保护行尾以%消除多余的空格。

答案2

在行末使用注释%并给出\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}

输出如下

在此处输入图片描述

答案3

我几乎重新接受了@Mu30 murugans2katgmail 的答案,并使用strut技巧添加了垂直填充(此处使用隐形规则创建):

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

在此处输入图片描述

您可以通过改变的值来控制字母上方和下方的间距\rule

编辑:正如建议的那样,如果您希望垂直填充精确居中,只需用以下内容\rule替换\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}

在此处输入图片描述

答案4

我继续前行后福拉克斯' 答案并以我自己的方式对其进行了增强。我不想永久更改\fboxsep整个文档,因此我添加了两个切换命令。也许这会在将来对某些人有所帮助!

梅威瑟:

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

相关内容