metanorma / tex2mn

Write Metanorma documents in LaTeX
https://www.metanorma.com
MIT License
2 stars 0 forks source link

Revise attributes mechanism #61

Open paolobrasolin opened 4 years ago

paolobrasolin commented 4 years ago

There currently are come catches in the straightforward mechanism we're using. E.g. to find forewords we're matching like @asciidoc-attributes='heading=foreword', coming from \mna{heading=forweord}, which is inherently frail.

Another ugly situation is with inline headers:

\subsection{Whatever,}
\mna{\%inline-header}

this does not look good.

Perhaps it would be nice to actually parse the attribute list using l3keys (see http://ctan.mirror.garr.it/mirrors/CTAN/macros/latex/contrib/l3kernel/interface3.pdf). However we must be very careful about edge cases -- that's why i passed the parameters verbatim until now.

How could we improve this without losing too much usability in the TeX API? The ergonomics right now aren't as good as they could be.

paolobrasolin commented 4 years ago

Here is an example of what could be done using l3keys:

\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

\keys_define:nn { metanorma } {
  heading .tl_set:N = \l_metanorma_canonical_heading_tl,
  heading .value_required:n = true,
  inline-header .bool_set:N = \l_mymodule_store_bool,
  inline-header .default:n = true,
  inline-header .value_forbidden:n = true,
}

\DeclareDocumentCommand \MetanormaHeading { o m } {
  \group_begin:
    \keys_set:nn { metanorma } { #1 }
    % NOTE: not really using \l_metanorma_canonical_heading_tl here.
    \bool_if:NTF \l_mymodule_store_bool {
      \group_insert_after:N \paragraph
    } {
      \group_insert_after:N \section
    }
  \group_end:
  { #2 }
}

\ExplSyntaxOff

\begin{document}
  \MetanormaHeading[heading=introduction]{Introduzione}
  Lorem ipsum.
  \MetanormaHeading[inline-header]{This thing,}
  it is neat!
\end{document}

Resulting in:

image

This would increase the complexity of Metanorma.cls a lot, especially if we want to preserve the well known macros instead of defining a bunch of new ones.

On the contrary, the approach of just using \lxRDF and \lxRDFa works very well to do just that: keep old macros and just attach easy to parse attributes at the XML level. The limitation would then be inherent in the evaluation order of a macro expansion language. Consider this:

\section{Foobar}
\lxRDFa{heading=abstract}

\section will never know its canonical name when parsed in TeX because \lxRDFa is called just too late. However \lxRDFa is exactly where we need it to attach the canonical heading as an attribute to the XML tree produced by latexml.

I think two key points are not inventing new macros when avoidable and absolutely not altering the parameters that well known macros can accept. That's why \lxRDF/\lxRDFa look very good to me.

However, we must answer two questions do conclusively decide what's the best course of action:

  1. which Metanorma attributes do we need to parse when using the TeX engine?
  2. is the increased complexity (technical in implementation and cognitive in usage) worth it?