1

I would like to take a string in Beamer and retrieve part of it. The solution at How do I split a string? gets me very close to what I want; but using it as example, i.e. if the string is:

\def\MyTeXKnowledge{Not good enough}

How can I retrieve an arbitrary word of this string, e.g. good? The output of the mentioned solution gives (Not)(good)(enough).

Thanks a lot,

2 Answers2

2
\documentclass{article} % or any other

\ExplSyntaxOn

\NewDocumentCommand{\getchunk}{smm}
 {% #1 = optional * (when a command is used for #2
  % #2 = string
  % #3 = item number
  \IfBooleanTF{#1}
   {
    \seq_set_split:NnV \l_tmpa_seq { ~ } #2
   }
   {
    \seq_set_split:Nnn \l_tmpa_seq { ~ } { #2 }
   }
  \seq_item:Nn \l_tmpa_seq { #3 }
 }

\ExplSyntaxOff

\newcommand{\MyTeXKnowledge}{Not good enough}

\begin{document}

\getchunk*{\MyTeXKnowledge}{2}

\getchunk{Not good enough}{3}

\end{document}

enter image description here

We might add a check that there are enough chunks.

\documentclass{article} % or any other

\ExplSyntaxOn

\NewDocumentCommand{\getchunk}{smm} {% #1 = optional * (when a command is used for #2 % #2 = string % #3 = item number \IfBooleanTF{#1} { \rigel_getchunk:Vn #2 { #3 } } { \rigel_getchunk:nn { #2 } { #3 } } }

\cs_new_protected:Nn \rigel_getchunk:nn { \seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 } \int_compare:nTF { #2 > \seq_count:N \l_tmpa_seq } { ??? \ERROR } { \seq_item:Nn \l_tmpa_seq { #2 } } } \cs_generate_variant:Nn \rigel_getchunk:nn { V }

\ExplSyntaxOff

\newcommand{\MyTeXKnowledge}{Not good enough}

\begin{document}

\getchunk*{\MyTeXKnowledge}{2}

\getchunk{Not good enough}{3}

\getchunk*{\MyTeXKnowledge}{100}

\end{document}

enter image description here

Also

! Undefined control sequence.
<argument> ???\ERROR

would be raised.

egreg
  • 1,121,712
  • Thanks a lot @egreg, but \ExplSyntaxOn is not available for me; maybe because I am using:

    pdfTeX 3.1415926-2.5-1.40.14 (TeX Live 2013) kpathsea version 6.1.1

    Unfortunately, I cannot update it by myself...

    – Rigel F. do C. Mar 27 '24 at 12:13
  • @RigelF.doC. Using an 11-year-old release? Do the sysadmins still use teleprinters or did they start to use modern devices such as green phosphor terminals? – egreg Mar 27 '24 at 12:47
1

The listofitems package is made to parse lists.

\documentclass{article}
\usepackage{listofitems}
\def\MyTeXKnowledge{Not good enough}
\begin{document}
\setsepchar{ }
\readlist\mylist\MyTeXKnowledge
Second item is: \mylist[2]

The list has \mylistlen{} items.

The last item is: \mylist[-1] \end{document}

enter image description here

  • Thanks a lot @Steven, but \usepackage{listofitems} is not available for me; maybe because I am using: pdfTeX 3.1415926-2.5-1.40.14 (TeX Live 2013) kpathsea version 6.1.1. Unfortunately, I cannot update it by myself... However, I will mark your reply as solution, since it is very concise – Rigel F. do C. Mar 27 '24 at 12:16
  • @RigelF.doC. You can obtain the sources at https://ctan.org/pkg/listofitems. If you are unable to install it in your system, merely placing the files listofitems.tex and listofitems.sty in your working directory will suffice. – Steven B. Segletes Mar 27 '24 at 12:23
  • 1
    Indeed! Thanks a lot @Steven! – Rigel F. do C. Mar 27 '24 at 13:59