3

How to set regular expressions for a character \\ correctly? MWE below produce unwanded extra space character between words with \c{\\}.

\documentclass[]{article}

\ExplSyntaxOn
\NewDocumentCommand{\ttl}{ m }
{
    \tl_set:Nn \l_title_tl {#1}
    \tl_set:Nn \l_tmpa_tl {#1}
    \regex_replace_all:nnN { \c{\\} } { } \l_tmpa_tl
    \tl_use:N  \l_tmpa_tl
}
\ExplSyntaxOff
\begin{document}

\ttl{One \\ Two}

One Two
\end{document}

Users may produce \ttl{One\\ Two} or \ttl{One \\ Two}. And I want just remove \\ and join words by single space.

enter image description here

3
  • In One \\ Two, there is one space token before \\ and one after. Seems to be working as designed.
    – frougon
    Jun 23 at 19:46
  • @frougon Users may produce \ttl{One\\ Two} or \ttl{One \\ Two}. And I want just remove `\` and join words by single space. Jun 23 at 19:58
  • 1
    urelated to your main question but a note that your title is misleading. In almost all standard contexts \\ does not end a paragraph, it forces a new line while keeping the current paragraph. (like Html <br> not like html> </p>`) Jun 24 at 21:19

3 Answers 3

3

In One \\ Two, there is one space token before \\ and one after; therefore, the regexp replacement is correct (\tl_show:N \l_tmpa_tl clearly shows two consecutive space tokens remaining in \l_tmpa_tl after the replacement).

Given your stated use case, I propose the following:

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand \ttl { m }
  {
    \tl_set:Nn \l_tmpa_tl {#1}
    \regex_replace_all:nnN { \ * \c{\\} \ * } { \ } \l_tmpa_tl
    % \tl_show:N \l_tmpa_tl % for checking
    \tl_use:N \l_tmpa_tl
  }
\ExplSyntaxOff

\begin{document}

\ttl{One \\ Two}

One Two
\end{document}

enter image description here

4

Spaces are not ignored after \\, so with your input \ttl{One \\ Two} you just remove \\, but the two space tokens remain. Besides, if the user types \ttl{One\\Two} you'd get no space.

So you want to transform a sequence of \\ preceded or followed by spaces into a single space.

\documentclass[]{article}

\ExplSyntaxOn
\NewDocumentCommand{\ttl}{ m }
  {
    \sergio_ttl:n { #1 }
  }

\tl_new:N \l__sergio_ttl_tl

\cs_new_protected:Nn \sergio_ttl:n
  {
    \tl_set:Nn \l__sergio_ttl_tl {#1}
    \regex_replace_all:nnN { \s* \c{\\} \s* } { \  } \l__sergio_ttl_tl
    \tl_use:N \l__sergio_ttl_tl
  }
\ExplSyntaxOff

\begin{document}

\ttl{One \\ Two}

One Two

\end{document}

enter image description here

If I do \tl_analysis_show:N instead of \tl_use:N, I get

The token list \l__sergio_ttl_tl contains the tokens:
>  O (the letter O)
>  n (the letter n)
>  e (the letter e)
>    (blank space  )
>  T (the letter T)
>  w (the letter w)
>  o (the letter o).

If you expect that your users might have \\* or \\[<dimen>] or the combination thereof, change the internal function into

\cs_new_protected:Nn \sergio_ttl:n
  {
    \tl_set:Nn \l__sergio_ttl_tl {#1}
    \regex_replace_all:nnN { \s* \c{\\}\s*(\*\s*|\[.*\])* \s* } { \  } \l__sergio_ttl_tl
    \tl_use:N \l__sergio_ttl_tl
  }

This would also cover all the following cases:

\ttl{One \\ Two}
\ttl{One \\[1ex] Two}
\ttl{One \\ [1ex] Two}
\ttl{One \\* Two}
\ttl{One \\ * Two}
\ttl{One \\*[1ex] Two}
\ttl{One \\* [1ex] Two}
\ttl{One \\ * [1ex] Two}

and of course all those with no space after One or before Two.

3

The following definition ignores the space after \\ because the #2 of the \ttlA is unseparated parameter: TeX ignores spaces in this case.

\def\ttl#1{\ttlA#1\\\end}
\def\ttlA#1\\#2{#1\ifdim\lastskip=0pt \space\fi\ifx\end#2\else\afterfi{\ttlA#2}\fi}
\def\afterfi#1#2\fi{\fi#1}

\ttl{One \\ Two}

\ttl{One\\ Two}

\ttl{One \\Two}

\ttl{One\\Two}

One Two

\bye

All these examples gives the same result: single space between One Two.

3
  • 1
    Thank you. By I prefer realize it in LaTeX3 Jun 23 at 19:59
  • @sergiokapone I prefer TeX primitive solution because it does not depend on LaTeX. And you can see that this solution is very simple and if tracingall is activated then you can see that my solution does much less operations. And it is closer to TeX basis. All problems discussed here is simply TeX.
    – wipet
    Jun 23 at 20:03
  • 1
    I have nothing against your answer and against plain TeX. It's just part of code that's already been written in LaTeX3 Jun 23 at 20:06

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.