# Finding and displaying UTF-8 characters with xesearch

The xesearch package allows one to replace text with other text. E.g.:

\documentclass{ctexbook}
\usepackage{fontspec}
\usepackage{xesearch}
\SearchList{list1}{newspaper}{book}
\begin{document}
The book costs \$1. \end{document}  When the document compiles, it reads, "The newspaper costs$1." When I tried other languages (e.g. \SearchList{list1}{书}{book}), it prints "The costs $1." (with a blank space where the character should appear), however, the document has no difficulty displaying the same character "书" elsewhere in the document. How can I replace foreign characters? Update: The code manages to successfully replace the characters, however, the lines aren't breaking in the replacement text: \documentclass[a5paper]{ctexbook} \pdfpagewidth=148mm \pdfpageheight=210mm \usepackage{fontspec} \setmainfont{Adobe Song Std} \setCJKmainfont{Adobe Song Std} \usepackage{xesearch} \SearchList{list1}{床前明月光，疑是地上霜。举头望明月，低头思故乡。}{poem} \begin{document} \Huge \noindent 床前明月光，疑是地上霜。举头望明月，低头思故乡。 \\ \noindent poem \\ \end{document}  - Of course you have specified a font that has the 书 glyph, haven't you? Please, make a complete example. – egreg Jan 20 '12 at 13:52 ## 2 Answers Add \makexeCJKactive at the beginning of the second argument of \SearchList. This works: \documentclass[a5paper]{ctexbook} \pdfpagewidth=148mm \pdfpageheight=210mm \usepackage{fontspec} \setmainfont{Adobe Song Std} \setCJKmainfont{Adobe Song Std} \usepackage{xesearch} \SearchList{list1}{\makexeCJKactive 床前明月光，疑是地上霜。举头望明月，低头思故乡。}{poem} \begin{document} \Huge \noindent 床前明月光，疑是地上霜。举头望明月，低头思故乡。 \\ \noindent poem \\ \end{document}  Warning: I haven't looked into xesearch package to do enough tests. Any two different packages that use \XeTeXinterchartoks heavilly may not work together well. I'm not sure about xesearch. - This works: \documentclass{article} \usepackage{fontspec} \setmainfont{Kai} \usepackage{xesearch} \SearchList{list1}{书}{book} \begin{document} The book costs \$1.
\end{document}


The current font must contain the glyph. If it doesn't, then ask XeLaTeX to use one that does:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\newfontfamily{\kanjifont}{Kai}
\usepackage{xesearch}
\SearchList{list1}{{\kanjifont 书}}{book}
\begin{document}
The book costs \\$1.
\end{document}


(I've used a main font that shows more clearly the difference.)

For longer strings you have to reset the locale. At least with this code the result in the two lines is identical:

\documentclass[a5paper]{ctexbook}
\pdfpagewidth=148mm
\pdfpageheight=210mm
\usepackage{fontspec}
\setmainfont{Kai}
\setCJKmainfont{Kai}
\usepackage{xesearch}
\def\1{\XeTeXlinebreaklocale "zh" \XeTeXlinebreakskip = 0pt plus 20pt\relax}

\SearchList{list1}{{\1床前明月光，疑是地上霜。举头望明月，低头思故乡。}}{poem}
\begin{document}
\Huge
\noindent 床前明月光，疑是地上霜。举头望明月，低头思故乡。 \\
\noindent poem \\
\end{document}


But some expert in xeCJK might help better.

However, xesearch is not the right tool for this: a macro \poem expanding to 床前明月光，疑是地上霜。举头望明月，低头思故乡。 would be more useful.

-
The examples seem to have difficulty with kerning. For e.g., \SearchList{list1}{书书书书书书书书书书书书书书书书书书}{book} results in the characters running off the edge of the page, –  Village Jan 20 '12 at 14:29
@Village There's no hyphenation and no space between the Chinese characters, so this is normal, I'd say. –  egreg Jan 20 '12 at 14:44
Chinese characters never have spaces between, but xeCJK or something should telling the characters to make a break. –  Village Jan 24 '12 at 1:17