1

This is actually a follow-up / the real question of this previous question.

I would like to convert text start with >> into centered text.

The motivation for this seemingly silly purpose would be to add a new syntax to my package jwjournal, so that lines like >> (some remark) would be converted into centered annotations.

Suppose that the string to be parsed has been stored into a token list \l_jwjournal_tmp_tl. The goal is to recognize >> and make the text after it centered. However, if multiple >> are found, the corresponding text should be centered separately. Thus for example, if the content of \l_jwjournal_tmp_tl is aaa>>bbb>>ccc, I would like bbb and ccc to be centered.

This previous question already addressed the main issue of parsing this token list properly. The problem for me now is that \l_jwjournal_tmp_tl shall be used later for further parsing and I would thus like the parsed result to directly overwrite \l_jwjournal_tmp_tl (just like \regex_replace_all:nnN which directly modifies the value of the given token list). How should I achieve this?

Below is a MWE.

\documentclass{article}
\begin{document}
\ExplSyntaxOn

\newenvironment{JWJournalCompactCenter} % https://tex.stackexchange.com/a/98893
  {\parskip=0pt\par\nopagebreak\centering}
  {\par\noindent\ignorespacesafterend}
\NewDocumentCommand \JWJournalCompactCenterText { m }
  {
    \begin{JWJournalCompactCenter}
      #1
    \end{JWJournalCompactCenter}
  }


\tl_new:N \l_jwjournal_tmp_tl
\tl_set:Nn \l_jwjournal_tmp_tl { aaa >>bbb >>ccc }

% \tl_new:N \l__mymodule_replace_head_tl
% \seq_new:N \l__mymodule_replace_tail_seq

% \cs_new_protected:Nn \mymodule_replace:n
%   {
%     \regex_split:nnN { >> } { #1 } \l__mymodule_replace_tail_seq
%     \seq_pop_left:NN \l__mymodule_replace_tail_seq \l__mymodule_replace_head_tl
%     \tl_use:N \l__mymodule_replace_head_tl
%     \seq_map_function:NN \l__mymodule_replace_tail_seq \JWJournalCompactCenterText
%   }

\tl_use:N \l_jwjournal_tmp_tl

\ExplSyntaxOff
\end{document}
1
  • I think you're overexploiting l3regex...
    – Skillmon
    Commented Aug 29, 2023 at 4:25

3 Answers 3

1

Instead of directly printing via \tl_use:N and \tl_map_function:NN you could simply put the result of those steps into your token list:

\documentclass{article}
\begin{document}
\ExplSyntaxOn

\newenvironment{JWJournalCompactCenter} % https://tex.stackexchange.com/a/98893
  {\parskip=0pt\par\nopagebreak\centering}
  {\par\noindent\ignorespacesafterend}
\NewDocumentCommand \JWJournalCompactCenterText { m }
  {
    \begin{JWJournalCompactCenter}
      #1
    \end{JWJournalCompactCenter}
  }


\tl_new:N \l_jwjournal_tmp_tl
\tl_set:Nn \l_jwjournal_tmp_tl { aaa >>bbb >>ccc }

\seq_new:N \l__mymodule_replace_tail_seq

\cs_new_protected:Nn \mymodule_replace:n
  {
    \regex_split:nnN { >> } { #1 } \l__mymodule_replace_tail_seq
    \seq_pop_left:NN \l__mymodule_replace_tail_seq \l_jwjournal_tmp_tl
    \seq_map_inline:Nn \l__mymodule_replace_tail_seq
      {
        \tl_put_right:Nn \l_jwjournal_tmp_tl
          { \JWJournalCompactCenterText {##1} }
      }
  }

\tl_use:N \l_jwjournal_tmp_tl

\ExplSyntaxOff
\end{document}
1
  • Thank you so much! I completely forgot that there is a \seq_map_inline:Nn...
    – Jinwen
    Commented Aug 29, 2023 at 8:18
2

I thing that if you have an accumulated text in (say) \tmpb macro, then you can print it in the "centering" mode (without changing it) or you can use it in another way. You needn't change it.

At TeX primitive level, the example can looks like this:

\def\printcenter#1>>#2\end{#1\par \ifx\relax#2\relax\else \afterfi{\docenter#2\end}\fi}
\def\docenter#1>>#2{\centerline{#1}\ifx\end#2\else\afterfi{\docenter#2}\fi}
\def\afterfi#1#2\fi{\fi#1}


\def\tmpb{aaa>>bbb>>ccc}
\expandafter\printcenter \tmpb>>\end % prints aaa at the left, bbb, ccc at the center

\def\tmpb{xxx}
\expandafter\printcenter \tmpb>>\end % prints xxx at the left
1

Here's a combined solution (just a proof of concept, change the functions to your liking).

\documentclass{article}
\begin{document}

\ExplSyntaxOn

\NewDocumentCommand \somecommand { m }
  {
    \jinwen_somecommand:n { #1 }
  }

\NewDocumentCommand \replace { m }
  {
    \jinwen_replace_use:n { #1 }
  }

\tl_new:N \l__jinwen_replace_head_tl
\seq_new:N \l__jinwen_replace_tail_seq

\cs_new:Nn \jinwen_somecommand:n { \,---\, #1 \,---\, }

\cs_new_protected:Nn \jinwen_replace_use:n
  {
    \__jinwen_split:n { #1 }
    \tl_use:N \l__jinwen_replace_head_tl
    \seq_map_function:NN \l__jinwen_replace_tail_seq \jinwen_somecommand:n
  }
\cs_new_protected:Nn \jinwen_replace_save:N
  {
    \__jinwen_split:V #1
    \tl_set_eq:NN #1 \l__jinwen_replace_head_tl % the head
    \seq_map_inline:Nn \l__jinwen_replace_tail_seq
     {
      \tl_put_right:Nn #1 { \jinwen_somecommand:n { ##1 } }
     }
  }

\cs_new_protected:Nn \__jinwen_split:n
  {
    \seq_set_split:Nnn \l__jinwen_replace_tail_seq { >> } { #1 }
    \seq_pop_left:NN \l__jinwen_replace_tail_seq \l__jinwen_replace_head_tl
  }
\cs_generate_variant:Nn \__jinwen_split:n { V }

\ExplSyntaxOff

X\replace{}X

\replace{aaa}

\replace{aaa>>bbb}

\replace{aaa>>bbb>>ccc}

\ExplSyntaxOn

\tl_new:N \l_jinwen_test_tl
\tl_set:Nn \l_jinwen_test_tl { aaa>>bbb>>ccc }

\jinwen_replace_save:N \l_jinwen_test_tl

\tl_show:N \l_jinwen_test_tl

\ExplSyntaxOff

\end{document}

As you see in the log file, the token list variable is changed as expected.

> \l_jinwen_test_tl=aaa\jinwen_somecommand:n {bbb}\jinwen_somecommand:n {ccc}.
2
  • Thank you for this! Just one more question: for \tl_set_eq:NN, do I have to worry if \l__jinwen_replace_head_tl might be changed later? There doesn't seem to be a \tl_set_eq:NV.
    – Jinwen
    Commented Aug 29, 2023 at 12:51
  • @Jinwen That’s \let, so no worry.
    – egreg
    Commented Aug 29, 2023 at 13:02

You must log in to answer this question.

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