3

Background

An XML document contains TeX elements in inline- and paragraph forms:

<p>Inline: <tex>$E=mc^3$</tex></p>
<p><tex>$$E=mc^2$$</tex></p>

I'd like to conditionally use placeformula with a cross-reference when $$ starts the <tex> element.

System

ConTeXt version: 2023.09.26 18:19

Problem

Everything I've tried to extract the first characters ends up with a compile failure.

Code

Here's the base setup:

\startxmlsetups xml:tex
  \xmlflushcontext{#1}
\stopxmlsetups

This works to generate the document, obviously without a cross-reference. Here are some attempts to obtain the leading characters (all failed):

\getfirstcharacter{\xmltext{#1}{.}}
\firstcharacter
% ...

\getfirstcharacter{\expanded{\xmlverbatim{#1}}}
\firstcharacter
% ...

\getfirstcharacter{\xmlflush{#1}}
\firstcharacter
% ...

\thefirstcharacter{\xmlflush{#1}}
\firstcharacter
% ...

\setvariable{Reference}{xml}{\xmlflush{#1}}
\getvariable{Reference}{xml}

\doiffirstcharelse{\xmlflush{#1}}{\textdollar}{DOLLAR}{NO DOLLAR}
\doiffirstcharelse{\xmltext{#1}}{\textdollar}{DOLLAR}{NO DOLLAR}

Plus numerous variations thereof. Using doiffirstcharelse won't work because I need to check the first two characters (or a substring), but gets the idea across.

Question

How would you extract the content of the XML tex element and check for a double-dollar sign to conditionally apply separate commands?

1 Answer 1

3

\doifinstringelse combined with \getsubstring appears to work:

\startbuffer[main]
<body>
    <p>Inline: <tex>$E=mc^3$</tex></p>
    <p><tex>$$E=mc^2$$</tex></p>
</body>
\stopbuffer

\startxmlsetups xml:root
    \xmlsetsetup{\xmldocument}{*}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:root}

\startxmlsetups xml:body
    \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:p
    \xmlflush{#1}
    \par
\stopxmlsetups

\startxmlsetups xml:tex
    \doifinstringelse%
        {$$}% Pattern
        {\getsubstring{1}{2}{\xmlflushcontext{#1}}}% Text
        {True}% True code
        {False}% False code

    \xmlflushcontext{#1}
\stopxmlsetups

\startTEXpage[offset=1dk, width=3in, align=middle]
    \xmlprocessbuffer{main}{main}{}
\stopTEXpage

output

I found these commands by searching for "string" in the command reference manual.

2

You must log in to answer this question.

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