定义 \newcommand 以将颜色合并到 \myvariables 中

定义 \newcommand 以将颜色合并到 \myvariables 中

我使用 LaTeX 来撰写求职申请。由于我有 50 多个职位要申请,我喜欢定义变量,例如:\targetorg、\website、\mysignature 等。由于这些是信件的敏感部分,我希望能够定义这些变量,使它们很容易脱颖而出。(例如通过颜色。)到目前为止,我一直在做以下事情:

\newcommand{\mycolor}{\color{blue}}  
\newcommand{\defaultcolor}{\color{black}}
\newcommand{\targetorg}{\mycolor "Name of organization I am applying work for" \defaultcolor}
\newcommand{\myname}{\mycolor "my name" \defaultcolor}
\newcommand{\job}{\mycolor "type of work" \defaultcolor}
\newcommand{\place}{\mycolor "location of organization" \defaultcolor}

然后可以按如下方式使用:

begin{document}
Hi,  
My name is \myname and would like to apply as \job for \targetorg in \place. 
end{document}

这样,当我编译文档时,各种定义的变量就会突出显示,这样我就可以快速发现是否有任何混乱并进行相应调整。在仔细检查工作完成后,我将 \mycolor 重新定义为黑色,以便文本具有统一的颜色。我现在这样做的方式非常完美,只是从编码的角度来看,总是必须为每个新的 \newcommand 实例包含 {\mycolor ... \defaultcolor} 看起来很原始。
问题:
我想问一下是否有办法创建一个 \newcommand,它会自动将这些颜色定义合并到自身中。最终,我想执行以下操作: \newcmdcolor{\targetorg}{"Name of organization I am applying work for"}
这应该完全等同于:
\newcommand{\targetorg}{\mycolor "Name of organization I am applying work for" \defaultcolor}
那么如何定义这样的 \newcmdcolor?
\newcommand{\newcmdcolor}{??????}

平均能量损失
我上面讨论了代码的基本方面。为了符合 MWE 标准,我特此制作了一个完整的工作示例,您可以将其复制粘贴以方便使用:

\documentclass{article}
\usepackage{xcolor}
\newcommand{\mycolor}{\color{blue}}  
\newcommand{\defaultcolor}{\color{black}}
\newcommand{\targetorg}{\mycolor "Dummy Organization" \defaultcolor}
\newcommand{\myname}{\mycolor "Dummy Name" \defaultcolor}
\newcommand{\job}{\mycolor "Dummy Work" \defaultcolor}
\newcommand{\place}{\mycolor "Dummy Location" \defaultcolor}

\begin{document}
\noindent
To whom it may concern,\\\indent
My name is \myname\, and I would like to apply for \job\, at \targetorg\, in \place.\\
Sincerely,\\
\myname
\end{document}

答案1

像这样吗?

只要说出来\newcolorcmd{category}{"Text"}你就会得到一个新命令\category,它会输出相应的文本。

基本上也可以使用托比昂T的命题

但是,不鼓励使用相同的颜色\mycolor,因此命令中应该有一个附加参数。

\documentclass{scrartcl}

\usepackage{xcolor}
\usepackage{etoolbox}


\newcommand{\mycolor}{blue}  
\newcommand{\defaultcolor}{black}
%\newcommand{\targetorg}{\mycolor "Name of organization I am applying work for" \defaultcolor}
%\newcommand{\myname}{\mycolor "my name" \defaultcolor}
%\newcommand{\job}{\mycolor "type of work" \defaultcolor}
%\newcommand{\place}{\mycolor "location of organization" \defaultcolor}

\newrobustcmd{\newcolorcmd}[2]{%
\long\csgdef{#1}{\textcolor{\mycolor}{#2}}%
}%

\newcolorcmd{job}{"Type of work"}%
\newcolorcmd{myname}{"My name"}%


\begin{document}


\job

\myname%


\end{document}

在此处输入图片描述

答案2

关键是使用\textcolor,而不是\color和 切换回默认颜色。当您不想再为字符串着色时,添加开关将允许轻松重新格式化。

使用该\perhapscolorize宏可以避免在不需要时使用颜色(因此\textcolor{black}当为假时将不会执行任何操作\ifcolorize

\newcommand{\newcmdcolor}[3]{% #1 is the command, #2 is the color, #3 is the text
  \newcommand{#1}{\perhapscolorize{#2}{#3}}%
}

\makeatletter
% The helper macro
\newcommand{\perhapscolorize}[1]{%
  \ifcolorize
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\textcolor{#1}}%
  {\@firstofone}%
}
\makeatother
% the conditional
\newif\ifcolorize

% comment the following if you don't want colors
\colorizetrue

完整的示例,显示您可以使用和打开或关闭颜色\colorizetrue;但是,您可能只会在序言中\colorizefalse注释掉。\colorizetrue

\documentclass{article}
\usepackage{xcolor}

\newcommand{\newcmdcolor}[3]{% #1 is the command, #2 is the color, #3 is the text
  \newcommand{#1}{\perhapscolorize{#2}{#3}}%
}

\makeatletter
% The helper macro
\newcommand{\perhapscolorize}[1]{%
  \ifcolorize
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\textcolor{#1}}%
  {\@firstofone}%
}
\makeatother
% the conditional
\newif\ifcolorize

% comment the following if you don't want colors
\colorizetrue

\newcmdcolor{\targetorg}{blue}{Name of organization I am applying work for}
\newcmdcolor{\myname}{red}{my name}
\newcmdcolor{\job}{green}{type of work}
\newcmdcolor{\place}{black!50}{location of organization}

\begin{document}

Hi, My name is \myname{} and would like to apply as \job{} for \targetorg{} in \place. 

\colorizefalse

Hi, My name is \myname{} and would like to apply as \job{} for \targetorg{} in \place. 

\end{document}

在此处输入图片描述

不同的版本,其中特定命令的颜色仅当与以下内容不同时才会说明\mycolor(必须先前定义):

\documentclass{article}
\usepackage{xcolor}

\newcommand{\newcmdcolor}[3][\mycolor]{%
  % #1 is the (optional) color, #2 is the command, #3 is the text
  \newcommand{#2}{\perhapscolorize{#1}{#3}}%
}
\newcommand{\mycolor}{blue}

\makeatletter
% The helper macro
\newcommand{\perhapscolorize}[1]{%
  \ifcolorize
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\textcolor{#1}}%
  {\@firstofone}%
}
\makeatother
% the conditional
\newif\ifcolorize

% comment the following if you don't want colors
\colorizetrue

% the first three will use \mycolor, the last one a different color
\newcmdcolor{\targetorg}{Name of organization I am applying work for}
\newcmdcolor{\myname}{my name}
\newcmdcolor{\job}{type of work}
\newcmdcolor[red]{\place}{location of organization}

\begin{document}

Hi, My name is \myname{} and would like to apply as \job{} for \targetorg{} in \place. 

\colorizefalse

Hi, My name is \myname{} and would like to apply as \job{} for \targetorg{} in \place. 

\end{document}

在此处输入图片描述

答案3

到目前为止,我还不知道您的 documentclass 或任何其他相关问题。但是我想从我的角度提供一些有用的提示。

  1. 我将定义一个全局的 if 子句来设置颜色。
  2. 我不会使用,而是\newcommand{\targetorg}定义一个\targetorg使用组织的命令,例如:\targetorg{Name of Org}

\documentclass[]{article}
\usepackage{xcolor,xspace}
\newif\ifhighlightsensitive
\highlightsensitivetrue
\highlightsensitivefalse

\let\mycolor\relax
\definecolor{mycolorname}{named}{black}
\ifhighlightsensitive
  \definecolor{mycolorname}{named}{blue}
\fi

\makeatletter

\let\@targetorg\relax
\newcommand{\targetorg}[1]{\gdef\@targetorg{#1}}
\newcommand\printtargetorg{\textcolor{mycolorname}{\@targetorg}\xspace}

\let\@myname\relax
\newcommand{\myname}[1]{\gdef\@myname{#1}}
\newcommand\printmyname{\textcolor{mycolorname}{\@myname}\xspace}

\let\@job\relax
\newcommand{\job}[1]{\gdef\@job{#1}}
\newcommand\printjob{\textcolor{mycolorname}{\@job}\xspace}

\let\@place\relax
\newcommand{\place}[1]{\gdef\@place{#1}}
\newcommand\printplace{\textcolor{mycolorname}{\@place}\xspace}
\makeatother

\targetorg{Name of organization I am applying work for}
\myname{my name}
\job{type of work}
\place{location of organization}
\begin{document}
Hi,  

My name is \printmyname and would like to apply as \printjob for \printtargetorg in \printplace. 

\end{document}

相关内容