1

This question is specfically about an expandable version of \IfStrEqCase (with multiple matches) as per Modified \IfStrEqCase with multiple matches per case.

However, in case this is an x-y problem, my real problem is to be able to ouptut a plain ascii version of the output that I can get in the PDF. I have been able to redefine most of my other macros to obtain the desired results, but am stuck on this one. Thus, am open to other solutions that don't require me to redefine all the macros as expandable versions.

The MWE below produces the desired results in the PDF:

enter image description here

but the .txt file contains:

Intro text... \par \IfStringCaseXO {case 4}{{{case 1}{case 3}{case 4}}{some 
long text which is identical for case 1, case 3 and case 4.} {{case 2}{case 5}}{other text 
for case 2 and case 5.}}\par Conclusion text...

but I desire this to be

Intro text... 
some long text which is identical for case 1, case 3 and case 4.
Conclusion text...

Notes:

  • As \DeclareExpandableDocumentCommand/\RenewExpandableDocumentCommand do not seem to allow an optional last parameter, the modified version \RenewExpandableDocumentCommand{\IfStringCaseXO} (commented out below) has different arg specs. While this could be a problem in general, I plan not to use the optional parameter when using \IfStringCaseXO in this one particular mode. However, if there is a way to allow a trailing optional parameter, it would be nice.

  • This is a separate question, but in case the solution is easy: How do I eliminate the \par in the output? I could of course do that with an external script, but if it is easy to do before ouptuting the content that would be great.

Code:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
%% --------------------------------------------------------------------------
%% https://tex.stackexchange.com/questions/350815/modified-ifstreqcase-with-multiple-matches-per-case
\cs_new_protected:Nn \grill_str_multicase:nnTF
 {
  \seq_set_split:Nnn \l__grill_str_multicase_cases_seq { } { #2 }
  \tl_clear:N \l__grill_str_multicase_cases_tl
  \int_step_inline:nnnn { 1 } { 2 } { \seq_count:N \l__grill_str_multicase_cases_seq }
   {
    \seq_set_split:Nnx \l__grill_str_multicase_subcases_seq { }
     { \seq_item:Nn \l__grill_str_multicase_cases_seq { ##1 } }
    \seq_map_inline:Nn \l__grill_str_multicase_subcases_seq
     {
      \tl_put_right:Nx \l__grill_str_multicase_cases_tl
       {
        {\exp_not:n{####1}}{\seq_item:Nn \l__grill_str_multicase_cases_seq { ##1 + 1}}
       }
     }
   }
   \str_case:nVTF { #1 } \l__grill_str_multicase_cases_tl { #3 } { #4 }
 }
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }
\cs_new_protected:Nn \grill_str_multicase:nn
 {
  \grill_str_multicase:nnTF { #1 } { #2 } { } { }
 }
\cs_new_protected:Nn \grill_str_multicase:nnT
 {
  \grill_str_multicase:nnTF { #1 } { #2 } { #3 } { }
 }
\cs_new_protected:Nn \grill_str_multicase:nnF
 {
  \grill_str_multicase:nnTF { #1 } { #2 } { } { #3 }
 }
\cs_generate_variant:Nn \grill_str_multicase:nn { o }
\cs_generate_variant:Nn \grill_str_multicase:nnF { o }

\NewDocumentCommand{\IfStringCaseX}{mmo}
 {
  \IfNoValueTF{#3}
   { \grill_str_multicase:nn { #1 } { #2 } }
   { \grill_str_multicase:nnF { #1 } { #2 } { #3 } }
 }
\NewDocumentCommand{\IfStringCaseXO}{mmo}
 {
  \IfNoValueTF{#3}
   { \grill_str_multicase:on { #1 } { #2 } }
   { \grill_str_multicase:onF { #1 } { #2 } { #3 } }
 }
 %% --------- End of definitions form https://tex.stackexchange.com/questions/350815
 %% --------------------------------------------------------------------------

\newcommand{\RedefineIfStringCaseXO}{%
    %\RenewExpandableDocumentCommand{\IfStringCaseXO}{mm}
    %{
    %    %% ??????
    %}
}%
\ExplSyntaxOff

\DeclareExpandableDocumentCommand{\ConditionalText}{m}{%
    Intro text...

    \IfStringCaseXO{#1}{%
      {{case 1}{case 3}{case 4}}{%
        some long text which is identical for case 1, case 3 and case 4.%
      }
      {{case 2}{case 5}}{%
        other text for case 2 and case 5.%
      }%
      %% Since expandable macros can't have an optional last parameter, 
    }%%[{Error: Unknown parameter ``#1'' to ConditionalText}]%
    \par Conclusion text...%
}


\newwrite\tempfile

\AtEndDocument{\immediate\closeout\tempfile}
\newcommand{\WriteToTextFile}[1]{%
    \RedefineIfStringCaseXO%
    \let\par\relax% <--- Don't want \par in the output file.
    \edef\OutputText{#1}%
    \immediate\openout\tempfile=\jobname.txt%
    \immediate\write\tempfile{\OutputText}%
    \immediate\closeout\tempfile%
}%


\begin{document}

\def\casesix{case 4}
\ConditionalText{\casesix}%% PDF output is fine
\WriteToTextFile{\ConditionalText{\casesix}}%% Desire ASCII output here.

\end{document}
  • Imho you have conflicting requirements. On the one side you want your command to expand fully in a write, on the other side you are using commands which are not expandable like ` \seq_set_split:Nnn`. – Ulrike Fischer Nov 23 '17 at 17:52
  • @UlrikeFischer: I got the above code form the linked question: Modified \IfStrEqCase with multiple matches per case and don't fully understand the expl syntax. – Peter Grill Nov 26 '17 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.