scrlttr2 - 如何将名字和姓氏的字体颜色分别更改为每个单词的不同颜色(在发件人字段中)?

scrlttr2 - 如何将名字和姓氏的字体颜色分别更改为每个单词的不同颜色(在发件人字段中)?

众所周知,发件人的姓名会自动显示在发件人字段内,其值取自fromname's变量(如下所示)。

最小示例:

\documentclass{scrlttr2}

\usepackage[english, ngerman]{babel}

\begin{document}
\setkomavar{fromname}    {John Doe (sender)}
\setkomavar{fromaddress} {25th Doe's Avenue \\ 54321 Springfield}

\begin{letter}{%
        Maria Lae\\
        2th Coast Road\\
        12345 Milltown%
        }

    \opening{Dear Maria,}
             as you can see, I want to change the color of my name inside sender-field
             independent respectively for first name and family name.
        \bigskip\\
             Unfortuntely, I don't know how to solve this...
        \bigskip\\
             Do you have any idea?
    \closing{Sincerely yours}
\end{letter}

\end{document}

现在我想将显示的字体颜色firstnamefamilyname每个单词分别更改为不同的颜色。

实际上我唯一的想法是基于创建新的KOMA 变量,分别针对firstnamefamilyname

\newkomavar[sender-firstname]{fromfirstname}
\setkomavar{fromfirstname}{John}

\newkomavar[sender-familyname]{fromfamilyname}
\setkomavar{fromfamilyname}{Doe}

那么,下一个问题就是如何用fromname两个新创建的变量(fromfirstnamefromfamilyname)替换现有的-registration(在 sender-field 内)。

您有什么想法或其他解决方案吗?

非常感谢您的帮助!

答案1

如果您只想为第一个标题中的名字和姓氏添加颜色,您可以设置变量firsthead

\documentclass[parskip=full-]{scrlttr2}
\usepackage{xcolor}
\usepackage[english, ngerman]{babel}
% new variables and fonts
\newkomavar[sender-firstname]{fromfirstname}
\newkomafont{fromfirstname}{}
\newkomavar[sender-familyname]{fromfamilyname}
\newkomafont{fromfamilyname}{}

\begin{document}
% set variables
\setkomavar{fromfirstname}{John}
\setkomavar{fromfamilyname}{Doe}
\setkomavar{fromname} {\usekomavar{fromfirstname} \usekomavar{fromfamilyname}}
\setkomavar{fromaddress} {25th Doe's Avenue \\ 54321 Springfield}
% set firsthead
\setkomavar{firsthead}{%
  \begin{parbox}[b]{\textwidth}\raggedright
    {\usekomafont{fromname}\strut\ignorespaces
      {\usekomafont{fromfirstname}\strut\ignorespaces\usekomavar{fromfirstname}}
      {\usekomafont{fromfamilyname}\strut\ignorespaces\usekomavar{fromfamilyname}}%
    }\\
    {\usekomafont{fromaddress}\strut\ignorespaces\usekomavar{fromaddress}}
  \end{parbox}%
}
% set fonts
\addtokomafont{fromfirstname}{\color{red}}
\addtokomafont{fromfamilyname}{\color{blue}}

\begin{letter}{%
  Maria Lae\\
  2th Coast Road\\
  12345 Milltown%
}

\opening{Dear Maria,}
as you can see, I want to change the color of my name inside sender-field
independent respectively for first name and family name.

Unfortuntely, I don't know how to solve this

Do you have any idea?
\closing{Sincerely yours}
\end{letter}
\end{document}

在此处输入图片描述

相关内容