我想知道是否有可能为文本字符串中的每个第二个字母着色LaTeX
。我正在写一份关于解密维吉尼亚密码的报告,并且有一个文本字符串,我想让每个替代字母都涂上不同的颜色。
例如,字符串可能是“abcdefg”,我希望输出以蓝色显示“aceg”,以红色显示“bdf”,或类似内容。这在 中是否容易实现LaTeX
?
感谢你们!
答案1
这是一个expl3
用于交替颜色的版本,但不适用于带有空格的字符串(到目前为止)
\documentclass{article}
\usepackage{xparse}
\usepackage[x11names]{xcolor}
\ExplSyntaxOn
\newcommand{\colorstring}[3]{%
\str_set:Nn \l_tmpa_str {#3}% store the string to a string variable
% Now loop through the string variable and get each 'letter'
\int_step_inline:nnnn {1} {1} {\str_count:N \l_tmpa_str } {%
\int_if_odd:nTF{##1}{% Is the number odd → use the first colour
\textcolor{#1}{\str_item:Nn \l_tmpa_str {##1}}
}{% No, use the 2nd colour
\textcolor{#2}{\str_item:Nn \l_tmpa_str {##1}}
}%
}%
}
\ExplSyntaxOff
\begin{document}
\bfseries
\huge \colorstring{blue}{red}{abcdefgh}
\huge \colorstring{brown}{Green4}{ABCEDEFGH}
\end{document}
另一个版本,颜色交替
... 并提供两种以上的颜色,以及在环境中使用。但是,不允许有空行!到目前为止,还没有对可打印字符进行真正的测试。
\documentclass{article}
\usepackage{xparse}
\usepackage{environ}
\usepackage[x11names]{xcolor}
\ExplSyntaxOn
\NewDocumentCommand{\colorstring}{O{blue,red}+m}{%
\clist_set:Nn \l_tmpa_clist {#1}%
\int_zero:N \l_tmpa_int%
\str_set:Nx \l_tmpa_str {#2}%
\int_step_inline:nnnn {1} {1} {\str_count:N \l_tmpa_str } {%
\str_case_x:nnF {\str_item:Nn \l_tmpa_str {##1}} {%
{\space}{\space}
}{%
\int_compare:nNnTF {\l_tmpa_int } < {\clist_count:N \l_tmpa_clist } {
\int_incr:N \l_tmpa_int
}{%
\int_set:Nn \l_tmpa_int {\c_one}
}
\textcolor{\clist_item:Nn \l_tmpa_clist {\l_tmpa_int }}{\str_item:Nn \l_tmpa_str {##1}}
}
}
}
\ExplSyntaxOff
\NewEnviron{ColorLettersInternal}[1]{\colorstring[#1]{\BODY}} % Internal environment for `\Body`
\NewDocumentEnvironment{ColorLetters}{O{blue,red}}{%
\ttfamily%
\ColorLettersInternal{#1}%
}{%
\endColorLettersInternal%
}
\parindent=0pt
\begin{document}
\bfseries
\huge \colorstring{abcdefgh}
\huge \colorstring[brown,Green4,Aquamarine4]{ABCEDEFGH}
\colorstringnew[violet,Green4,Red4,Blue2]{TCGATGGAGGGACCAT}
\begin{ColorLetters}[Blue4,Red4,Green4,LightBlue4]
And now for something completely different
Number Three -- the Larch!
\end{ColorLetters}
\end{document}
答案2
这里,我使用了一个简单的递归,直到到达字符串的末尾。
\documentclass{article}
\usepackage{xcolor}
\newcommand\colorstring[3]{\def\colorA{#1}\def\colorB{#2}\colorstringhelp#3%
\relax\relax\relax}
\def\colorstringhelp#1#2#3\relax{\textcolor{\colorA}{#1}\textcolor{\colorB}{#2}%
\ifx\relax#3\else\colorstringhelp#3\relax\relax\fi}
\begin{document}%
\colorstring{blue}{red}{abcdef}\par
\colorstring{green}{orange}{abcdefg}
\end{document}
答案3
由于密文通常排版时不带空格,因此对输入进行映射很简单(并且会自动吞噬空格)。我们environ
可以定义一个收集其正文的环境。
\documentclass{article}
\usepackage{xparse}
\usepackage{environ}
\usepackage[x11names]{xcolor}
\ExplSyntaxOn
\seq_new:N \l_kaddon_colors_seq
\int_new:N \l_kaddon_color_int
\cs_new_protected:Nn \kaddon_color_string:nn
{
\seq_set_from_clist:Nn \l_kaddon_colors_seq { #1 }
\int_zero:N \l_kaddon_color_int
\tl_map_inline:nn { #2 } { \kaddon_color:n { ##1 } }
}
\cs_generate_variant:Nn \kaddon_color_string:nn { nV }
\cs_new_protected:Nn \kaddon_color:n
{
\int_incr:N \l_kaddon_color_int
\int_compare:nT { \l_kaddon_color_int > \seq_count:N \l_kaddon_colors_seq }
{ % exhausted colors, restart
\int_set:Nn \l_kaddon_color_int { 1 }
}
\str_if_eq:nnTF { #1 } { \par }
{
\int_decr:N \l_kaddon_color_int
\par
}
{
\textcolor{\seq_item:Nn \l_kaddon_colors_seq { \l_kaddon_color_int } } { #1 }
\hspace{0pt plus 0.1pt }
}
}
\NewDocumentCommand{\colorstring}{ O{red,blue} m }
{
\kaddon_color_string:nn { #1 } { #2 }
\unskip % remove the last \hspace
}
\NewEnviron{colorpar}[1][red,blue]
{
\begin{quote}
\kaddon_color_string:nV { #1 } \BODY
\end{quote}
}
\ExplSyntaxOff
\begin{document}
\colorstring{abcdefgh}
\colorstring[red,red,blue]{abcd efgh}
\colorstring[brown,Green4,Aquamarine4]{ABCEDEFGH abcdef}
\begin{colorpar}[Blue4,Red4,Green4,LightBlue4]
And now for something completely different
And now for something completely different
And now for something completely different
And now for something completely different
\end{colorpar}
\begin{colorpar}
And now for something completely different
And now for something completely different
And now for something completely different
And now for something completely different
\end{colorpar}
\end{document}