1つの単語を2つの異なる色で書く方法は?
Aug 17 2020
一言ありますが、2色で書きたいと思います。通常、2つの\textcolo
セクションの間にスペースがあります
\textcolor{sectcol}{la}\textcolor{white}{tex}
負の値でスペースを「削除」でき\hspace
ます。
\textcolor{sectcol}{la}\hspace{-1mm}\textcolor{white}{tex}
これを行うためのより良い方法はありますか?
回答
3 UlrikeFischer Aug 17 2020 at 16:54
カラーインサートと呼ばれるwhatits
、これはカーニングを防ぎます:
\documentclass{article}
\usepackage{color}
\begin{document}
VA
\textcolor{red}{V}\textcolor{blue}{A}
\end{document}

それについてできることはあまりありません(ネガティブスペースの挿入は別として)。それを本当に回避する唯一の方法は、lualatexエンジンとluacolorパッケージを使用することです。
\documentclass{article}
\usepackage{luacolor}
\begin{document}
VA
\textcolor{red}{V}\textcolor{blue}{A}
\end{document}

2 egreg Aug 17 2020 at 17:13
標準フォントでは「a」と「t」の間にカーニングがないため、この例は適切ではありません。
行き詰まってpdflatex
LuaTeXを使用できない場合は、必要なカーニングを手動で計算できます。
\documentclass{article}
\usepackage{xcolor,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\twocolors}{mmmm}
{% #1 = color for the first part
% #2 = first part
% #3 = color for the second part
% #4 = second part
\egreg_twocolors:nnnn { #1 } { #2 } { #3 } { #4 }
}
\box_new:N \l__egreg_twocolors_kern_box
\box_new:N \l__egreg_twocolors_nokern_box
\cs_new_protected:Nn \egreg_twocolors:nnnn
{
\hbox_set:Nn \l__egreg_twocolors_kern_box
{
\tl_item:nn { #2 } { -1 }
\tl_item:nn { #4 } { 1 } % could just be \tl_head:n
}
\hbox_set:Nn \l__egreg_twocolors_nokern_box
{
\hbox:n { \tl_item:nn { #2 } { -1 } }
\hbox:n { \tl_item:nn { #4 } { 1 } } % could just be \tl_head:n
}
\textcolor{#1}{#2}
\skip_horizontal:n
{
\box_wd:N \l__egreg_twocolors_kern_box - \box_wd:N \l__egreg_twocolors_nokern_box
}
\textcolor{#3}{#4}
}
\ExplSyntaxOff
\begin{document}
\twocolors{green!50!blue}{two}{red!30}{color}
twocolor
\makebox[0pt][l]{twocolor}\twocolors{green!50!blue}{two}{red!30}{color}
\makebox[0pt][l]{\twocolors{green!50!blue}{two}{red!30}{color}}twocolor
\twocolors{green!50!blue}{la}{red!30}{tex}
latex
\makebox[0pt][l]{latex}\twocolors{green!50!blue}{la}{red!30}{tex}
\makebox[0pt][l]{\twocolors{green!50!blue}{la}{red!30}{tex}}latex
\twocolors{green!50!blue}{V}{red!30}{A}
VA
\makebox[0pt][l]{VA}\twocolors{green!50!blue}{V}{red!30}{A}
\makebox[0pt][l]{\twocolors{green!50!blue}{V}{red!30}{A}}VA
\end{document}
各例には、色が漏れないことを確認しています。ただし、解像度が小さい場合、ラスタラーにアーティファクトが表示されることがあります。下の画像は非常に高い解像度で撮影されています。
制限:最初の部分の最後の項目または2番目の部分の最初の項目がアクセント付き文字である場合は、それ(またはそれら)を中括弧で囲みます。
