Is it possible to tweak how TeXmacs documents get converted to LaTex?

Conversion to LaTex is pretty decent, but I sometimes end up some very weird LaTex macros/environments. For instance, I get $V_{\tmop{DS}} \approx 2.25 \mathrm{V} when I expected it to be $V_{DS} \approx 2.25 \text{V}$. This is the sort of thing I can easily clean up with a script, but I wonder if I can directly change the way LaTex gets converted to from the source.

This answer applies: How to assign keyboard shortcut to a macro

I currently use a whole system that is comprised of scripts that do the cleanup.

guraltsev/template.texmacs-paper: A template for a texmacs paper with LaTeX conversion scripts (github.com)

In the TeXmacs file I use the tag

<specific|LaTeX|???>

to specify things to be exported into latex only.

Since there is no way to REMOVE things from the export, here is the strategy.

I export, and run the script

./.bin/texmacs2latex.sh outputfile.tex

This creates the file outputfile.out.tex to output the processed file

The scripts:

  1. Deletes the TeXmacs preamble,
  2. Deletes the TeXmacs bibliography,
  3. Includes a handmade latex preamble that, among other things, includes the local style files my-paper.sty, my-macros.sty, my-texmacs.sty
  4. Runs other scripts in the .bin\texmacs2latex.d\ directory
    For example it does some regex substitutions using sed to fix a bug in exproting parenthesis. You can add your own scripts there.

The workflow is then to compile the output tex file and if it doesn’t compile mannually add lines from the original output generated by TeXmacs to my-texmacs.sty. That way you have full control on how the TeXmacs-specific exports are rendered.

Here are the details of inner workings

I insert lines like

<specific|latex|% !!!begin[TeXmacs][preamble]>

at the beginning of the TeXmacs preamble.

Insert line

<specific|latex|% !!!end[TeXmacs][preamble]>

at the end of the preamble, followed by

<specific|latex|% !!!begin[LaTeX][document]>

<specific|latex|\documentclass[a4paper,10pt,intlimits,sumlimits]{amsart}>
<specific|latex|\usepackage{my-paper}>

<specific|latex|\usepackage{my-macros}>

<specific|latex|\usepackage{my-texmacs}>

<specific|latex|% !!!begin[LaTeX][bibliography]>

<specific|latex|\addbibresource{template.bib}>

<specific|latex|% !!!end[LaTeX][bibliography]>

and at the end.

At the end of the file, close to the bibliography, I put

<specific|latex|% !!!begin[TeXmacs][bibliography]>

<\bibliography|bib|tm-alpha|template.bib>
;

<specific|latex|% !!!end[TeXmacs][bibliography]>

<specific|latex|% !!!begin[LaTeX][bibliography]>

<specific|latex|\printbibliography>

<specific|latex|% !!!end[LaTeX][bibliography]>

I export, and run the script

./.bin/texmacs2latex.sh outputfile.tex

What this does is

  1. Creates the file outputfile.out.tex to output the processed file

  2. Deletes the TeXmacs preamble, i.e. everything up to the line

<specific|latex|% !!!end[TeXmacs][preamble]>

  1. Inserts the LaTeX header written in the specific tags
    this includes three style files (my-paper.sty, my-macros.sty, my-texmacs.sty)

  2. in the LaTeX header also includes the .bib file

  3. Deletes the TeXmacs bibliography and the end. It should be surrounded by

<specific|latex|% !!!begin[TeXmacs][bibliography]>

and

<specific|latex|% !!!end[TeXmacs][bibliography]>

Then it inserts a \printbibliography line for latex.

<specific|latex|% !!!begin[LaTeX][bibliography]>

<specific|latex|\printbibliography>

<specific|latex|% !!!end[LaTeX][bibliography]>

Runs the scripts in .bin\texmacs2latex.d\

1 Like

TeXmacs has a different semantics than LaTeX, by default DS in math mode means an operator and not the multiplication of two variables “D” and “S” (which is the meaning in TeX), so when converting it inserts an annotation to reproduce the semantics faithfully. If you do not want it then you have to enter differently the TeXmacs code, e.g. “D*S”. My experience with conversion with LaTeX is that there is not much to tweak. Instead of trying to make complex post-processing, it would be better to analyse the situation more carefully and understand why TeXmacs behaves in some ways. Sometimes it is a bug, but many other times there is a reason and working around just prevents you to take full advantage of the design of the program.

2 Likes