9

I want to change the color of a single letter inside a custom \spacedallcaps command. This command makes the specified text uppercase and changes the letter spacing.

When I introduce a \textcolor{color}{letter} it will simply not work.

Also, taking the letter out of the \spacedallcaps piece of text works only if the specified letter is at the end or at the beginning. But how can I manage this when the letter is in the middle of a word?

I tried the following :

\documentclass{article}
\usepackage{color}
\usepackage{textcase} % for \MakeTextUppercase
\usepackage{soul} % for letterspacing
\sodef\allcapsspacing{\upshape}{0.25em}{0.8em}{0.6em} % : {space between letters}{space between words}{0.6em}
\DeclareRobustCommand{\spacedallcaps}[1]{\MakeTextUppercase{\allcapsspacing{#1}}} % all caps with better spacing

\begin{document}
\spacedallcaps{One single colored letter}\\

\spacedallcaps{One single col}\hspace{0.25em}\textcolor{red}{O}\spacedallcaps{red letter}\\

\spacedallcaps{One single col}\hspace{0.25em}\textcolor{red}{O}\hspace{0.25em}\spacedallcaps{red letter}
\end{document}

But the result is far from being good :

result

One can always play with the \hspace function but it is extremely tedious as it requires endless trials.

How can one achieve this automatically?

12

Use xcolor instead of color and issue

\colorlet{RED}{red}

because \MakeTextUppercase also "uppercases" red inside \textcolor.

At this point (notice \textcolor inside braces)

\spacedallcaps{One single col{\textcolor{red}{O}}red letter}

would suffice.

MWE

\documentclass{article}
\usepackage{xcolor}
\usepackage{textcase} % for \MakeTextUppercase
\usepackage{soul} % for letterspacing
\sodef\allcapsspacing{\upshape}{0.25em}{0.8em}{0.6em} % : {space between letters}{space between words}{0.6em}
\DeclareRobustCommand{\spacedallcaps}[1]{\MakeTextUppercase{\allcapsspacing{#1}}} % all caps with better spacing

\colorlet{RED}{red}

\begin{document}
\spacedallcaps{One single col{\textcolor{red}{O}}red letter}
\end{document} 

Output

enter image description here


I would like to add a suggestion from Christopher for adjusting the spacing between letters.

\documentclass{article}
\usepackage{xcolor}
\usepackage[letterspace=150]{microtype}

\DeclareRobustCommand{\spacedallcaps}[1]{\MakeUppercase{\textsc{#1}}} % all caps with better spacing

\colorlet{RED}{red}

\begin{document}

\spacedallcaps{\lsstyle One single col\textcolor{red}{O}red letter}

\end{document} 
  • Neat! This works perfectly – jrojasqu Mar 6 '15 at 14:20
  • Perhaps you should explain, why RED is necessary ;-) (+1 anyway) – user31729 Mar 6 '15 at 14:21
  • @karlkoeller Just a small reminder : microtype will not work if one compiles with XeLaTeX or such. – jrojasqu Mar 7 '15 at 1:49
  • @jrojasqu I know, but I wanted to add Christopher's solution, who deleted his answer, which was intended as a comment on my answer. Letter spacing is supported only by pdfLaTeX and LuaLaTeX, but it's not a bad suggestion. Anyway I'm trying to undelete his answer, so I will remove that part from mine. :-) – karlkoeller Mar 7 '15 at 6:21
5

You can register commands for soul:

\documentclass{article}
\usepackage{color}
\usepackage{textcase} % for \MakeTextUppercase
\usepackage{soul} % for letterspacing
\sodef\allcapsspacing{\upshape}{0.25em}{0.8em}{0.6em} % : {space between letters}{space between words}{0.6em}
\DeclareRobustCommand{\spacedallcaps}[1]{%
  \MakeTextUppercase{\allcapsspacing{#1}}%
} % all caps with better spacing

\DeclareRobustCommand{\makered}[1]{\textcolor{red}{#1}}
\soulregister{\makered}{1}

\begin{document}
\spacedallcaps{One single col{\makered{o}}red letter}
\end{document}

enter image description here

4

Just wanted to see if this would work with my \spaceout macro, not using soul.

EDITED to provide for separate inter-word spacing.

Provides definitions \theLetterSpace and \theWordSpace as default inter-letter and inter-word spacing (Note that \theWordSpace is in addition to the natural \ space that accompanies word breaks). By default, I set \theWordSpace to 1.5 x the local letter-spacing, dynamically readjusted as \theLetterSpace changes. The local value of the letter spacing can also be changed with an optional argument to \spaceout, as shown in the MWE.

\documentclass{article}
\usepackage{xcolor}
\def\theLetterSpace{0.5pt}
\def\theWordSpace{1.5\LocalLetterSpace}
\newlength\LocalLetterSpace
\newcommand\spaceout[2][\theLetterSpace]{%
  \setlength\LocalLetterSpace{#1}\expandafter\spaceouthelpA#2 \relax\relax}
\def\spaceouthelpA#1 #2\relax{%
  \spaceouthelpB#1\relax\relax%
  \ifx\relax#2\else\ \kern\theWordSpace\spaceouthelpA#2\relax\fi
}
\def\spaceouthelpB#1#2\relax{%
  #1%
  \ifx\relax#2\else
    \kern\LocalLetterSpace\spaceouthelpB#2\relax%
  \fi
}
\parskip 1ex
\colorlet{RED}{red}
\begin{document}
test \uppercase{\spaceout[5pt]{one single col{\textcolor{red}{o}}red letter}} with 5pt added spacing

test \uppercase{\spaceout[3pt]{one single col{\textcolor{red}{o}}red letter}} with  3pt added spacing

test \uppercase{\spaceout[1pt]{one single col{\textcolor{red}{o}}red letter}} with  1pt added spacing

\def\theWordSpace{5\LocalLetterSpace}Change the inter-word spacing to 5 x local letter spacing.

test \uppercase{\spaceout[1pt]{one single col{\textcolor{red}{o}}red letter}} with  1pt added spacing
\end{document}

enter image description here

  • I am interested in your macro : how do you manage the word spacing? Can it be incremented or is it fixed? – jrojasqu Mar 6 '15 at 15:35
  • @jrojasqu The macro \spaceouthelpA parses word by word, while \spaceouthelpB parses each word character by character. Yes, I could add a separate inter-word space, and I will edit my MWE to implement that. – Steven B. Segletes Mar 6 '15 at 15:44
  • @jrojasqu Please see revision. – Steven B. Segletes Mar 6 '15 at 15:53
  • I did, it is great! Hopefully I can find an optimal word spacing with your macro. Using proportional spacing gives a more refined aspect. – jrojasqu Mar 6 '15 at 16:00
  • @jrojasqu I have made an addditional edit, when I realized that \theWordSpace should be based on \LocalLetterSpace and not \theLetterSpace. Please use this later version. – Steven B. Segletes Mar 6 '15 at 16:05

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.