1

I'm trying to print two different text if a string contains a ',' or not. The conditional is working, but the text is getting printing without spaces.

This is the code for the command:

\NewDocumentCommand {\dayeach} { O{} m}
  {
      \IfInteger{#1}
      {
        \IfSubStr{#2}{,}
            {#1 /day each:}
            {#1 /day}
      }
      {#1}
      %end if%
      {#2}

  }

And this is an example of how i call it:

\dayeach[3]{drink, eat}

with that i'm getting:

"3/dayeach: drink, eat""

1

You are using the expl3 syntax. After \ExplSyntaxOnallspacesareignoreduntilthe\ExplSyntaxOff. Inside \ExplSyntaxOn a space is produced with ~, so you need to use some of these:

\documentclass{article}
\usepackage{xparse}
\usepackage{xstring}
\ExplSyntaxOn
\NewDocumentCommand \dayeach { O{} m }
  {
    \IfInteger{#1}
      {
        \IfSubStr{#2}{,}
          {#1 /day~each:~}
          {#1 /day~}
      }
      {#1}
      {#2}
  }
\ExplSyntaxOff
\begin{document}
\dayeach[3]{drink, eat}

\dayeach[3]{sleep}
\end{document}

However, since you're using expl3 anyway, I'd suggest to use expl3's functions instead of xstring's:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \dayeach { O{} m }
  {
    \IfInteger{#1}
      {
        \str_if_in:nnTF {#2} {,}
          {#1 /day~each:~}
          {#1 /day~}
      }
      {#1}
      {#2}
  }
\NewExpandableDocumentCommand \IfInteger { m m m }
  { \xhark_if_integer:nTF {#1} {#2} {#3} }
\prg_new_conditional:Npnn \xhark_if_integer:n #1 { p, T, F, TF }
  {
    \tl_if_empty:fTF
      { \int_to_roman:n { -0#1 } }
      { \prg_return_true: }
      { \prg_return_false: }
  }
\cs_generate_variant:Nn \tl_if_empty:nTF { f }
\ExplSyntaxOff
\begin{document}
\dayeach[3]{drink, eat}

\dayeach[3]{sleep}
\end{document}

Both examples produce:

enter image description here

  • 1
    Thanks! it worked. I'm using overleaf, and aperently it doesn't like exp13 functions. – Xhark Jun 13 at 15:07
  • 1
    @Xhark Hm... Overleaf has a slightly outdated TeX distribution, and expl3 changed a lot in the past few years, so some things might not work. However I just tested the code and it worked fine. The only thing is the syntax highlighting that thinks that \xhark_if_integer:nTF is an error. But you can safely ignore that :-) – Phelype Oleinik Jun 13 at 15:13
  • 1
    Yet I'd prefer \tl_if_empty:fTF { \int_to_roman:n { -0#1 } } – egreg Jun 13 at 17:18

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.