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

5
  • 3
    Can you define "character" can you assume a single token in pdftex, or do you include multi-byte UTF-8 sequences. Commented Feb 21, 2020 at 14:17
  • Any character in utf8 sequences with xetex or luatex.
    – Salim Bou
    Commented Feb 21, 2020 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? Commented Feb 21, 2020 at 14:23
  • Oh you really should have stated unicode tex that makes nion ascii a lot easier:-) Commented Feb 21, 2020 at 14:24
  • @UlrikeFischer It is supposed that word has no ligature, I'll change the example.
    – Salim Bou
    Commented Feb 21, 2020 at 14:29

4 Answers 4

14

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.

5
  • Thanks David Carlisle, can you add little explanation on how macro work.
    – Salim Bou
    Commented Feb 21, 2020 at 14:40
  • 1
    @SalimBou well it just recursively calls itself until #1 is 1 then sets #2 in red. Commented Feb 21, 2020 at 14:53
  • Is there a way to make such macro (Format char and format chars) work with glyphs, accents? as sown in tex.stackexchange.com/questions/355666/…
    – Silva
    Commented Aug 15, 2020 at 23:03
  • @Silva better to ask questions a s a question post not as a comment on an old question. As I say here if you use xelatex or luallatex any uniocde character will work, but not clusters of combining characters. Commented Aug 15, 2020 at 23:07
  • @David tex.stackexchange.com/questions/558813/…
    – Silva
    Commented Aug 16, 2020 at 0:18
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

2
  • +1. :-) How would you have to change the code so that \colornth{4}{öäüßÖÄÜ} might run correctly under pdfLaTeX? (It runs fine under LuaLaTeX.)
    – Mico
    Commented Feb 22, 2020 at 18:29
  • 1
    @Mico That would be extremely tedious.
    – egreg
    Commented Feb 22, 2020 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

1
  • +1. How would you have to change the code so that \ColorNthChar{4}{öäüßÖÄÜ} might run correctly under pdfLaTeX? (It runs fine under LuaLaTeX...)
    – Mico
    Commented Feb 22, 2020 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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .