3

I'm using the awesome-cv class, which highlights the first three letters of the section heading like so:

Education with "Edu" highlighted

The code that produces this effect looks like this:

\RequirePackage{xcolor}
\RequirePackage{xifthen}
\RequirePackage{etoolbox}
\RequirePackage[quiet]{fontspec}
\RequirePackage{fontawesome}

\definecolor{awesome-red}{HTML}{DC3522}
\colorlet{awesome}{awesome-red}

\newcounter{colorCounter}
\def\@sectioncolor#1#2#3{%
  {%
    \color{%
      \ifcase\value{colorCounter}%
        awesome\or%
        awesome\or%
        awesome\or%
        awesome\or%
        awesome\else%
        awesome\fi%
    } #1#2#3%
  }%
  \stepcounter{colorCounter}%
}

The text size and boldface are added later in

\newcommand*{\sectionstyle}[1]{{\fontsize{15pt}{1em}\bodyfont\bfseries\color{text}\@sectioncolor #1}}

How does the first part of this code highlight the first three letters?


I understand that this isn't a MWE, but I wasn't able to create one out of the .cls file and I don't think this code is that complex to someone who knows what they're looking at. If I need and MWE, let me know.

  • The parameters to \@sectioncolor are the first three characters #1#2#3. Similar to how \frac12 produces the fraction 1/2. Note that when \@sectioncolor is called its parameters are not enclosed in a brace group. – Peter Grill Mar 23 '16 at 22:19
  • I thought that was the case. But what happens to the rest of it, and why is there a case statement? Taking out #3 doesn't change the highlighting to the first two characters, it causes a crash. – Azor Ahai Mar 23 '16 at 22:21
  • Did you also change the second #1#2#3 to #1#2 following the color? If you changed it #1#2#3 to #1#2 in both places it should highlight just the first two characters. After the first three characters are processed, \@sectioncolor has done its on and the remaining characters are just text that is not processed by ``\@sectioncolor. Same as $\frac123$ producing the fraction 1/2 followed by 3. – Peter Grill Mar 23 '16 at 22:25
  • Ah, it wasn't compiling because I had the .pdf open in another window, not because of my edits to the code. Thanks, though. – Azor Ahai Mar 23 '16 at 22:36
5

Actually, this trick is quite simple: Arguments to normal TeX commands can either be single tokens (i.e. characters or \controlsequences), or they can be token sequences enclosed in braces. For example this is why, in math mode, you can write \frac 1 2, \frac{1}{2} or even \frac1{2} and get the same result.

Therefore, all you need to do to get the first three characters of a word is a command with three arguments. For example,

\newcommand\mycommand[3]{%
    \textcolor{red}{#1}%
    \textcolor{green}{#2}%
    \textcolor{blue}{#3}%
}

allows you to highlight the first three letters following an invocation like \mycommand hello. The lo in this case is simply not part of the command.

To get the familiar syntax with just one parameter in braces, you can wrap this command in another command:

\newcommand*\myothercommand[1]{%
    \mycommand #1%
}

This allows you to write \myothercommand{hello}.

Here's a MWE that demonstrates a stripped-down version of the command with different colors:

\documentclass{article}
\RequirePackage{xcolor}
\RequirePackage{xifthen}

\definecolor{awesome-red}{HTML}{DC3522}
\colorlet{awesome}{awesome-red}

\newcounter{colorCounter}
\newcommand\sectioncolor[3]{%
  {%
    \color{%
      \ifcase\value{colorCounter}%
        awesome\or%
        green\or%
        blue\or%
        orange\or%
        yellow\else%
        cyan\fi%
    } #1#2#3%
  }%
  \stepcounter{colorCounter}%
}

\newcommand*{\sectionstyle}[1]{{\fontsize{15pt}{1em}\bfseries\sectioncolor #1}}

\begin{document}
    \sectionstyle{first}\par
    \sectionstyle{second}\par
    \sectionstyle{third}\par
    \sectionstyle{fourth}\par
    \sectionstyle{fifth}\par
    \sectionstyle{sixth}\par
    \sectionstyle{seventh}
\end{document}

colorful output

  • Ah, I understand now. That is a neat trick. But why is there a counter in the example? – Azor Ahai Mar 23 '16 at 22:25
  • It appears to be rather useless in this case. But if you insert different colors in the cases (instead of awesome for all of them), you can have it use different colors on the first few invocations. – wrtlprnft Mar 23 '16 at 22:27

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.