1

I have an issue I want to resolve, maybe you can help me.

When I compile the mwe the following happens:

In the PDF you can see: (X/A)(Up/Down)

In the file.txt write: (\frac {a}{x})(\frac {u}{d})

How can I write to the file what is seen in the PDF?

mwe:

\documentclass[border=1cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{expl3,xparse,xstring}

\ExplSyntaxOn \RenewDocumentCommand{\frac}{mm}{ \IfStrEqCase{#1}{{a}{X}}[Up] / \IfStrEqCase{#2}{{x}{A}}[Down] }

\NewDocumentCommand{\Write}{+m}{ \iow_new:N \tdm_iow \cs_gset:Npn \my_wrr:x ##1{ \iow_open:Nn \tdm_iow { file.txt } \iow_now:Nx \tdm_iow{##1} } \my_wrr:x {#1} } \ExplSyntaxOff

\begin{document} \def\str{(\frac{a}{x})(\frac{u}{d})} \str \Write{\str} \end{document}

Thanks for your help!

2 Answers2

0

Instead of xstring's non-expandable \IfStrEqCase use expl3's expandable \strcase:nnTF.
Instead of \RenewDocumentCommand use \RenewExpandableDocumentCommand.

(With an up-to-date TeX-installation you need neither \usepackage[utf8]{inputenc} nor \usepackage{expl3,xparse}.)

\documentclass[border=1cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{expl3,xparse}

\ExplSyntaxOn \RenewExpandableDocumentCommand{\frac}{mm}{ \str_case:nnTF{#1}{{a}{X}}{}{Up} / \str_case:nnTF {#2}{{x}{A}}{}{Down} }

\NewDocumentCommand{\Write}{+m}{ \iow_new:N \tdm_iow \cs_gset:Npn \my_wrr:x ##1{ \iow_open:Nn \tdm_iow { file.txt } \iow_now:Nx \tdm_iow{##1} } \my_wrr:x {#1} } \ExplSyntaxOff

\begin{document} \def\str{(\frac{a}{x})(\frac{u}{d})} \str \Write{\str} \end{document}

enter image description here

file.txt:

(X/A)(Up/Down)
Ulrich Diez
  • 28,770
0

I'm not sure why you would redefine \frac, so I'll use a different name.

  1. \iow_new:N should be outside \Write

  2. There is no need to set a function inside \Write

  3. \IfStrEq is not fully expandable.

\documentclass{article}
%\usepackage{xparse} % uncomment for older LaTeX

\ExplSyntaxOn \NewExpandableDocumentCommand{\FRAC}{mm} { \str_case:nnF { #1 } { {a}{X} } { Up } / \str_case:nnF { #1 } { {x}{A} } { Down } }

\iow_new:N \rodriguez_out_iow

\NewDocumentCommand{\Write}{O{\c_sys_jobname_str.txt}m} { \rodriguez_write:nn { #1 } { #2 } }

\cs_new:Nn \rodriguez_write:nn { \iow_open:Nn \rodriguez_out_iow { #1 } \iow_now:Nx \rodriguez_out_iow { #2 } } \ExplSyntaxOff

\begin{document}

\def\str{(\FRAC{a}{x})(\FRAC{u}{d})}

\str

\Write{\str}

\end{document}

The file \jobname.txt will contain

(X/Down)(Up/Down)

The PDF will be

enter image description here

egreg
  • 1,121,712