2

I need a command to replace (erase) the characters · and | from a string.

Consider the following naive code:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{stringstrings}

\newcommand\mycommand[1]{%
  \convertchar[q]{#1}        {·}{}%
  \convertchar[v]{\thestring}{|}{}}

\begin{document}
\mycommand{foo·bar|baz}
\end{document}

It gives a stack exceeded error; I think the reason is that the occurrences of · are passed as \IeC {\textperiodcentered } to \convertchar.

Also, the substitution of | is not working. I think that's due to the font encoding as suggested by the manual of stringstrings.

Fiddling with \unexpanded and \detokenize got me nowhere: I don't quite get what is happening, so I just made a mess.

What is the best way to write such a command? Why?

(An implementation using xstring is equally welcome; I tried, obtaining essentially the same results.)

3

The problem is, as far as I can see, that stringstrings tries expansion and that · is a two byte character.

I'd suggest expl3 and \tl_remove_all:Nn that doesn't attempt expansion.

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand\mycommand{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \tl_remove_all:Nn \l_tmpa_tl { · }
  \tl_remove_all:Nn \l_tmpa_tl { | }
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\begin{document}
\mycommand{foo·bar|baz}
\end{document}

enter image description here

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.