5

I use LaTeX to write my job applications. Since I have more than 50 places to apply for, I like to define variables, such as: \targetorg, \website, \mysignature etc. Since these are sensitive portions of the letters, I would like to be able to define these variables such that they easily can stick out.(by means of a color for example.) So far what I have been doing is the following:

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

This would then be used as follows:

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

In this manner, when I compile my document, the various defined variables stick out so I can spot quickly if there are any mess ups and adjust accordingly. After I am done with double checking the work, I then redefine \mycolor to black so that the text is in one uniform color. The way I have done it now works perfectly, except that from a coding perspective it looks primitive to always have to include {\mycolor ... \defaultcolor} for every new instance of \newcommand.
Question:
I would like to ask if there is a way to make a \newcommand that would automatically incorporate these color definitions within itself. Ultimately I would like to do the following: \newcmdcolor{\targetorg}{"Name of organization I am applying work for"}
which should be the exact equivalent of:
\newcommand{\targetorg}{\mycolor "Name of organization I am applying work for" \defaultcolor}
So how do a define such a \newcmdcolor?
\newcommand{\newcmdcolor}{??????}

MWE
I discussed above the essential aspects of the code. For the sake of complying to the MWE standards, I hereby produce a full working sample which can be copy-pasted to your convenience:

\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}
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 May 31 '14 at 10:35
  • 1
    Welcome to TeX.SX! The \textcolor command will perhaps make things look a bit better, i.e. \newcommand\newcmdcolor{\textcolor{\mycolor}{Name of organization}}. – Torbjørn T. May 31 '14 at 10:36
2

Something like this?

Just say \newcolorcmd{category}{"Text"} and you will get a new command \category which will output the corresponding text.

Basically one could also use Torbjorn T's. proposition

Using the same color for \mycolor is not encouraged however, so there should be an additional argument to the command.

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

enter image description here

4

The key is using \textcolor, rather than \color and switching back to the default color. Adding a switch will allow for easy reformatting when you don't want to color the strings any more.

The use of the \perhapscolorize macro avoids using colors when not wanted (so no \textcolor{black} will be performed when \ifcolorize is false.

\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

Complete example, showing that you can turn colors on and off with \colorizetrue and \colorizefalse; however, you'll probably just comment out the \colorizetrue in the preamble.

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

enter image description here

A different version, where the color for a particular command is stated only if different from \mycolor (which must be previously defined):

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

enter image description here

  • Thank you for the suggestions. I will implement the ideas provided, however it is not answering the actual question I had. My question revolves around avoiding to use a color command in every new variable that needs to be defined. I need a new version of \newcommand which Christian Hupher provided. – Gnam May 31 '14 at 11:04
  • 1
    @Gnam I'll add a different version that uses a standard color by default, but can accept a different one. – egreg May 31 '14 at 11:10
1

As far I don't know your documentclass or any other related question. However I want to provide some useful hints from my point of view.

  1. I would define a global if clause which setup the color.
  2. Instead of using \newcommand{\targetorg} I would define a command \targetorg, which uses the organisation, e.g.: \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}
  • Could you please explain to me why point number two would be more efficient? – Gnam May 31 '14 at 11:06
  • @Gnam: So you can manipulate every string in your manner. – Marco Daniel May 31 '14 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.