7

How to create a macro with two arguments the first representing the order of character in the word and the second is the word itself.

\documentclass{article}
\usepackage{xcolor}

\begin{document}

\def\ColorNthChar#1#2{....} % color #1 nth char in #2  

% example of use 

\ColorNthChar{3}{examination}

\end{document}

And the output will be in the form

enter image description here

  • 3
    Can you define "character" can you assume a single token in pdftex, or do you include multi-byte UTF-8 sequences. – David Carlisle Feb 21 at 14:17
  • Any character in utf8 sequences with xetex or luatex. – Salim Bou Feb 21 at 14:20
  • 4
    Your input has 14 characters, the output has 13 glyphs as the fi builds a ligature. How should that be counted? – Ulrike Fischer Feb 21 at 14:23
  • Oh you really should have stated unicode tex that makes nion ascii a lot easier:-) – David Carlisle Feb 21 at 14:24
  • @UlrikeFischer It is supposed that word has no ligature, I'll change the example. – Salim Bou Feb 21 at 14:29
13

Assuming "character" means "a single character token in input" (so just ASCII with pdftex but Unicode input with xetex or luatex) then

\documentclass{article}
\usepackage{xcolor}

\begin{document}

\makeatletter
\def\ColorNthChar#1#2{\xColorNthChar{#1}#2\@empty}
\def\xColorNthChar#1#2{\ifnum\ifx\@empty#21\else#1\fi=1 \textcolor{red}{#2}\expandafter\@gobbletwo
    \else#2\fi\xColorNthChar{\numexpr#1-1\relax}}
\makeatother
% example of use 

\ColorNthChar{3}{classification}  \ColorNthChar{23}{examination}  \ColorNthChar{8}{examination}

\end{document}

updated version that checks if the word is too short.

|improve this answer|||||
  • Thanks David Carlisle, can you add little explanation on how macro work. – Salim Bou Feb 21 at 14:40
  • 1
    @SalimBou well it just recursively calls itself until #1 is 1 then sets #2 in red. – David Carlisle Feb 21 at 14:53
13

A simple implementation in expl3:

\documentclass{article}

\usepackage{xparse,xcolor}

\ExplSyntaxOn

\NewDocumentCommand{\colornth}{O{red}mm}
 {% #1 = color to use, #2 = position, #3 = word
  % deliver all characters before the chosen position
  \tl_range:nnn { #3 } { 1 } { #2 - 1 }
  % deliver the character in the chosen position with the desired color
  \textcolor{#1}{ \tl_item:nn { #3 } { #2 } }
  % deliver all characters after the chosen position
  \tl_range:nnn { #3 } { #2 + 1 } { -1 }
 }

\ExplSyntaxOff

\begin{document}

\colornth{3}{examination}

\colornth[blue]{-3}{examination}

\end{document}

With negative numbers you start counting from the end.

enter image description here

Just for fun, how to color differently several characters.

\documentclass{article}

\usepackage{xparse,xcolor}

\ExplSyntaxOn

\NewDocumentCommand{\colornth}{O{red}mm}
 {% #1 = color to use, #2 = number, #3 = word
  \tl_range:nnn { #3 } { 1 } { #2 - 1 }
  \textcolor{#1}{ \tl_item:nn { #3 } { #2 } }
  \tl_range:nnn { #3 } { #2 + 1 } { -1 }
 }
\NewDocumentCommand{\colorsome}{mm}
 {% #1 = list of chars to color, #2 = word
  % split the given token list
  \seq_set_split:Nnn \l_tmpa_seq { } { #2 }
  % with \seq_indexed_map_inline:Nn we have ##1=item number, ##2=item
  \seq_indexed_map_inline:Nn \l_tmpa_seq
   {
    \textcolor{ \int_case:nnF { ##1 } { #1 } { . } }{ ##2 }
   }
 }

\ExplSyntaxOff

\begin{document}

\colornth{3}{examination}

\colornth[blue]{-3}{examination}

\colorsome{{3}{red}{6}{blue!75!red}}{examination}

\end{document}

The first argument to \colorsome should be a list of pairs {number}{color}; since \int_case:nnF is fully expandable, at the end we will get either the color for a chosen position, or . which denotes the current color.

enter image description here

|improve this answer|||||
  • +1. :-) How would you have to change the code so that \colornth{4}{öäüßÖÄÜ} might run correctly under pdfLaTeX? (It runs fine under LuaLaTeX.) – Mico Feb 22 at 18:29
  • 1
    @Mico That would be extremely tedious. – egreg Feb 22 at 20:19
10
\documentclass{article}
\usepackage{xcolor,xparse}

\begin{document}
\ExplSyntaxOn
\NewDocumentCommand\ColorNthChar {mm}
{ \int_zero:N\l_tmpa_int
  \tl_map_inline:nn
   {
    #2
   }
   {
    \int_incr:N\l_tmpa_int
    \int_compare:nNnTF {\l_tmpa_int}={#1}
     {
       \textcolor{red}{##1}
     }
     {
       ##1
     }
   }
}
\ExplSyntaxOff    
% example of use

% example of use

\ColorNthChar{3}{examination}
\ColorNthChar{23}{examination}
\ColorNthChar{8}{examination}

\end{document}

enter image description here

|improve this answer|||||
  • +1. How would you have to change the code so that \ColorNthChar{4}{öäüßÖÄÜ} might run correctly under pdfLaTeX? (It runs fine under LuaLaTeX...) – Mico Feb 22 at 18:32
2

A little bit late to the game, but here's a LuaLaTeX-based solution. The solution is set up to work with non-ASCII-encoded characters such as öäüßÖÄÜ.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{xcolor}
%% set up the Lua function that does the actual work:
\directlua{
function color_nth_char ( n , s )
  local t
  t = unicode.utf8.sub(s,1,n-1) ..
      "\string\\textcolor{red}{" .. unicode.utf8.sub(s,n,n) .. "}" ..
      unicode.utf8.sub(s,n+1) 
  tex.sprint ( t )
end
}
%% set up a LaTeX utility macro that invokes the Lua function:
\newcommand\ColorNthChar[2]{\directlua{color_nth_char(#1,"#2")}}  

\begin{document}
\ColorNthChar{3}{examination}

\ColorNthChar{4}{öäüßÖÄÜ}
\end{document}
|improve this answer|||||

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.