2

I am using xelatex and trying to italicize a text segment that includes a comma. The only way ahead that I know of is by using xesearch and its command SearchList. It works for the most part, but if the phrase includes a comma I simply can't seem to pass the entire phrase as a single argument to the command (by escaping the comma). Here is an example of what I am trying to do:

\documentclass[11pt, a4paper]{article}
\usepackage{xesearch}
\begin{document}
  a, b, abc, b, a, b, c

  \SearchList*{list1}{\textit{#1}}{a, b}

  a, b, abc, b, a, b, c

  \StopList{list1}
\end{document} 

Doing this matches the a's but does not italicize the b's anywhere. The culprit seems to be the space in front of the b that is passed as an argument to SearchList. Removing the space so that the line now reads

\SearchList*{list1}{\textit{#1}}{a,b}

results in the single a and b's being italicized (as expected).

However, I am specifically trying to italicize the segment "a, b" (with a comma and a space between the letters), ignoring other instances of the letter b. All the occurrences of "a, b" -- two in this case -- should be italicized.

I have tried adding braces following the advice found here: how do I escape a comma? although I must admit that I don't fully understand escape sequences in latex. This is what I have tried:

\documentclass[11pt, a4paper]{article}
\usepackage{xesearch}
\begin{document}
a, b, abc, b, a, b, c

\UndoBoundary{,}
\SearchList*{list1}{\textit{#1}}{{a, b}}

a, b, abc, b, a, b, c

\StopList{list1}
\MakeBoundary{,}
\end{document}

This seems to not do anything. Any help would be appreciated.

  • I don't think it's possible to use the space in a search string; one can remove , from the delimiters, but spaces are different. – egreg Aug 8 '17 at 20:30
4

I don't know the xesearch package but you could use expl3 regex:

enter image description here

\documentclass[11pt, a4paper]{article}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new_protected:Npn \zz #1
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_replace_all:nnN {a,\s b}{\c{textit}\cB\{\0\cE\}} \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\begin{document}

a, b, abc, b, a, b, c

\zz{a, b, abc, b, a, b, c}

\end{document}
  • Thanks for your answer. I seem to end up with an ! Undefined control sequence. \zz ...t:Nn \l_tmpa_tl {#1}\regex_replace_all:nnN {a,\s b}{\c {textit}\cB \{... while trying to compile the code (with xelatex or pdflatex). Could you help me here? – Jishnu Aug 9 '17 at 5:45
  • The undefined control sequence goes away and the code works if we use \usepackage{l3regex} instead of \usepackage{expl3} – Jishnu Aug 9 '17 at 5:48
  • 1
    @Jishnu l3regex used to be an extension package for expl3 so you have an old version, but that's OK: the old package should work here. If you update your tex system there will be no l3regex package and the functionality has been included in the base expl3 code. – David Carlisle Aug 9 '17 at 8:11
0

A listofitems approach.

\documentclass[11pt, a4paper]{article}
\usepackage{listofitems}
\begin{document}
a, b, abc, b, a, b, c

\setsepchar{a, b}
\readlist\mylist{a, b, abc, b, a, b, c}
\foreachitem\i\in\mylist{\ifnum\icnt>1\relax\textit{\mylistsep[\icnt]}\fi\i}
\end{document} 

enter image description here

In macro form, it could be done as such:

\documentclass[11pt, a4paper]{article}
\usepackage{listofitems}
\newcommand\mysearch[2][a, b]{%
  \setsepchar{#1}%
  \readlist\mylist{#2}%
  \foreachitem\i\in\mylist{\ifnum\icnt>1\relax\textit{\mylistsep[\icnt]}\fi\i}%
}
\begin{document}
a, b, abc, b, a, b, c

\mysearch{a, b, abc, b, a, b, c}

\mysearch[abc, b]{a, b, abc, b, a, b, c}
\end{document} 

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.