michal-h21 / make4ht

Build system for tex4ht
140 stars 15 forks source link

ODT with fancy equation numbers, a Plain TeX alignment, and TikZ in a table #56

Open gl-utah opened 2 years ago

gl-utah commented 2 years ago

The LaTeX file "FancyEqnNumbers_TikZTable.tex"

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\makeatletter
\def\eqalignno#1{\displ@y \tabskip\@centering
  \halign to\displaywidth{\hfil$\@lign\displaystyle{##}$\tabskip\z@skip
    &$\@lign\displaystyle{{}##}$\hfil\tabskip\@centering
    &\llap{$\@lign##$}\tabskip\z@skip\crcr
    #1\crcr}}
\makeatother

\def\enum#1(#2:#3){\refstepcounter{equation} \label{#1#2}
              \eqno{\mathrm{(#1\theequation#3)}}}
\def\enuma#1(#2:#3){\refstepcounter{equation}
             \label{#1#2}\mathrm{(#1\theequation#3)}}
\def\eoldnum#1(#2:#3){\eqno{\mathrm{(#1\ref{#1#2}#3)}}}

\begin{document}
Beginning
$$ T  \,,\enum(entropy:)$$% Equation (1)
expression
$$\frac{a}{b}  \enum B(multinomial:)$$% Equation (B2)
and
$$ y \eoldnum (entropy:')$$% Equation (1')
also
$$ w \eoldnum B(multinomial:')$$% Equation (B2')
finally
$$\eqalignno{x &= 2 &\enuma(align1:)\cr y &= 4\,.\cr}$$% Equation (3)

In
\begin{tabular}{r|c|c}
& a & b \\ \hline
&
\begin{tikzpicture}\begin{axis}[ymin=1,xmin=0,ymax=3,xmax=4]
\node at (1,2) {$\bullet$};
\end{axis}\end{tikzpicture}%
&
\\ \hline
\end{tabular}
\end{document}

produces fancy_correct but make4ht -c config.cfg -e build.lua -f odt FancyEqnNumbers_TikZTable.tex ends with a fatal error unless one comments out the lines of the tikzpicture and the \eqalignno. After commenting out those lines, one obtains an ODT file which appears in LibreOffice as fancy (Microsoft Word 2016 and 2019 cannot open this ODT file). None of the equation numbers are properly placed, and two of the equations are undisplayed. Is there any way to fix these problems and get the tikz and \eqalignno lines to work? (The build.lua and config.cfg files I use are attached below; I gave them .TXT extensions to enable the uploads.) build.lua,txt config.cfg,txt

michal-h21 commented 2 years ago

This works better:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\makeatletter
\def\eqalignno#1{\displ@y \tabskip\@centering
  \halign to\displaywidth{\hfil$\@lign\displaystyle{##}$\tabskip\z@skip
    &$\@lign\displaystyle{{}##}$\hfil\tabskip\@centering
    &\llap{$\@lign##$}\tabskip\z@skip\crcr
    #1\crcr}}
\makeatother

\def\enum#1(#2:#3){\refstepcounter{equation} \label{#1#2}
              \eqno{({\mathrm{#1}\theequation}#3)}}
\def\enuma#1(#2:#3){\refstepcounter{equation}
             \label{#1#2}({\mathrm{#1\theequation}}#3)}
\def\eoldnum#1(#2:#3){\eqno{({\mathrm{#1\ref{#1#2}}#3)}}}

\ifdefined\HCode%
\newenvironment{picmath}{\Picture*{}$$}{$$\EndPicture}
\else
\newenvironment{picmath}{$$}{$$}
\fi

\begin{document}
Beginning
$$ T  \,,\enum(entropy:)$$% Equation (1)
expression
$$\frac{a}{b}  \enum B(multinomial:)$$% Equation (B2)
and
$$ y \eoldnum (entropy:')$$% Equation (1')
also
$$ w \eoldnum B(multinomial:')$$% Equation (B2')
finally

% $$\eqalignno{x &= 2 &\enuma(align1:)\cr y &= 4\,.\cr}$$% Equation (3)
\begin{picmath}
\eqalignno{x &= 2 &\enuma(align1:)\cr y &= 4\,.\cr}% Equation (3)
\end{picmath}

In
\begin{tabular}{r|c|c}
& a & b \\ \hline
& 
~ 
\begin{tikzpicture}\begin{axis}[ymin=1,xmin=0,ymax=3,xmax=4]
\node at (1,2) {$\bullet$};
\end{axis}\end{tikzpicture}%
&
\\ \hline
\end{tabular}
\end{document}

I've fixed your math macros, and introduced new environment: picmath. It is needed for your \eqalignno command, because it produces picture in math otherwise and it cannot be displayed.

I found that the Tikzpicture is removed by some table post-processing script (not make4ht post-processing, but other mechanism, called Xtpipes. It is more low-level and I don't really understand how it works). As a workaround, I found that it is not removed when you put some other content to the same table cell, like ~. The picture is still not displayed correctly, probably due the fact, that it is larger than the declared cell width.

This is the result: Snímek z 2021-12-13 14-07-30

gl-utah commented 2 years ago

For some reason, on my machine make4ht throws a fatal error when given the input file with your modifications. The command line make4ht -c config.cfg -e build.lua -f odt -m draft -a debug FancyEqnNumbers_TikZTableMH.tex > debug.txt with input file FancyEqnNumbers_TikZTableMH.tex.txt (delete the .txt extension) gives rise to debug.txt, whose first error message is error_message

michal-h21 commented 2 years ago

This file don't fail for me, but the commands that are reported as missing are provided by the Graphics package. Try to add it to your document, to see if it helps.

The config.cfg file that I tried is this:

\Preamble{xhtml}
\catcode`\:=11
\Configure{MathClass}{1}{*}{<\a:mathml mi\Hnewline \mml:class="MathClass-op">}{</\a:mathml mi><mo> &\#x2061;<!--FUNCTION APPLICATION--></mo>}{}
\catcode`\:=12
\begin{document}
\EndPreamble
gl-utah commented 2 years ago

Yes, I think that's the same config.cfg: config.cfg.txt My build.lua file is: build.lua.txt My LaTeX file, now calling for the Graphicx package, is: FancyEqnNumbers_TikZTableMHg.tex.txt The command make4ht -c config.cfg -e build.lua -f odt -m draft -a debug FancyEqnNumbers_TikZTableMHg.tex > debug.txt generates this log file: debug.txt This reports the same errors as before I added the line \usepackage{graphicx} to the LaTeX file. My make4ht package was "packaged on" 10/14/21; my latex-graphics-dev was packaged on 11/1/2021. Perhaps something needs to be added to the config.cfg file?

michal-h21 commented 2 years ago

And what version is ooffice.4ht (you can find it using kpsewhich ooffice.4h). The current version is from 2021-12-13. Try to update TeX4ht packages.

gl-utah commented 2 years ago

kpsewhich ooffice.4ht pointed to a file with date April 16, 2021. I updated my MiKTeX installation fully (using the MiKTeX Console), but that file's date did not change, and the overall behavior did not change. (The new debug file is: debug.txt.) How can I get needed updates if they are not offered by the MiKTeX console?

ThiloteE commented 2 years ago

@gl-utah There is an option to download pre-release packages. See here:

image

You can enable it and check if there are updates available for your packages.

I personally was not able to download an update only for a single specific package. Since i wanted to get the latest update for make4ht, I had to download pre-release updates for ALL my packages, which i am not sure is a good thing to do. I also am not sure if that was mandatory, or if i overlooked some kind of setting that would allow me to be more specific, but if you are set on getting the latest updates, you might want to make use of this option.

gl-utah commented 2 years ago

@ThiloteE Thanks for alerting me to that possibility. Upon choosing that option in the MiKTeX console, I get a list of pre-release packages which have checkmarks which cannot be unchecked. For example, in MiKTeXConsole_update I can uncheck the one package labeled as "optional" (as I did) but I cannot uncheck any of the other packages.

The web page https://miktex.org/kb/miktex-next says MiKTeXupdate_reverse so if no one suggests an alternative way of getting the newest make4ht, I may try this perhaps-not-so-risky route. Too bad the checkboxes in the MiKTeX Console sometimes cannot be deselected, like most checkboxes can.

michal-h21 commented 2 years ago

I don't really know much about Miktex, but I guess that this pre-release distribution should be similar to up-to date TeX Live, where everything except binaries is kept in sync with CTAN? I always keep my TL up-to-date, and it only occasionally causes issues, when some package breaks other packages.

I think that .4ht files in the normal Miktex distro are too old, so you should try the pre-release, especially if it is possible to roll back in case of problems.

gl-utah commented 2 years ago

In the MiKTeX Console, I went to Packages, then right-clicked to remove tex4ht and remove make4ht. Then I went to Updates, "Change", and checked "Retrieve pre-release (experimental) packages". Next, I chose a CTAN server. (I didn't actually update.) Then I went back to Packages, then right-clicked and installed tex4ht (packaged June 4, 2021) and make4ht (packaged October 14, 2021). CTAN, at https://ctan.org/pkg/make4ht, has a version of 0.3j, 2021-10-11, so I think that's the package I got. No date is listed at CTAN for https://ctan.org/pkg/tex4ht. Unfortunately, when I checked the ooffice.4ht file, its date remained April 16, 2021. The file make4ht.exe is dated December 9, 2021. In the MiKTeX Console, I went to Updates, Check for Updates, and got many "release state change" items, reflecting my switch to getting pre-release packages, but nothing for tex4ht or make4ht. So it seems this is as updated as I can get this MiKTeX installation for tex4ht and make4ht.

I decided to rewrite my macros without using so many Plain TeX ideas, and to simplify things as much as possible. I spent some days learning some of the AMSmath package.

In the program below, the definition of \enum is simpler than I require, but as a first step, it would be nice to get this version to work:

\documentclass{article}
\usepackage{amsmath}
\def\enum#1(#2:#3){\refstepcounter{equation} \label{#1#2}
              \tag{#1\theequation#3}}

\begin{document}
See
\[ T  \enum(shouldbeOne:) \]
\end{document}

The LaTeX output is See but running make4ht and opening the ODT file in LibreOffice unfortunately yields See2 Opening the ODT file in Microsoft Word gives the same result, as does using LibreOffice to export the file to .DOCX format and then opening it in MS Word.

So, is there any way to get the equation number set correctly?

michal-h21 commented 2 years ago

The TeX4ht sources are not in CTAN, they go directly to TeX Live. Miktex takes them from TL, but only few times a year, so they can be quite old.

Regarding your example, it is not that easy to get what you want in ODT output. You can try the following configuration:

\Preamble{xhtml}
\catcode`\:=11
\Configure{equation*}
  {\ifvmode \IgnorePar\fi \EndP
%
   \HCode{<table:table table:style-name="equation">%
       <table:table-column table:style-name="equ-col" />\Hnewline
       <table:table-column table:style-name="equ-num-col" />\Hnewline
       <table:table-row>%
       <table:table-cell table:style-name="equ-cell">%
       <text:p text:style-name="equ-p">}%
   \Configure{$$}
   {\Configure{@math}{}\IgnorePar\EndP
    \CenteredFrametrue \DviMath \CenteredFramefalse }
   {\EndDviMath\EndP}{}%
%
   \IgnorePar
\def\EqNum{~}% default value of the number. It cannot be empty, the table would be eaten otherwise
  }
  {%
\ifvmode \IgnorePar\fi\EndP \HCode{</text:p></table:table-cell>\Hnewline
       <table:table-cell table:style-name="equ-num-cell">%
       <text:p text:style-name="equ-num-p">}%
\EqNum%
\HCode{</text:p></table:table-cell>\Hnewline
          </table:table-row></table:table>}%
   \IgnoreIndent\par}
\catcode`\:=12

\begin{document}
% redefine enum
\def\enum#1(#2:#3){\refstepcounter{equation} \label{#1#2}
              \gdef\EqNum{(#1\theequation#3)}}
\EndPreamble

It reuses the code used for the equation environment, so it can produce the equation number. The \enum command is redefined to define the \EqNum command, which then contains the stuff that should be aligned to the right.

The downside is, that it will be used for every display math, even when you don't want any equation number. A safer solution would be to use a custom environment just for these tagged equations.

gl-utah commented 2 years ago

I really appreciate your working on this.

I had thought that the difficulties came about because I was using ideas from Plain TeX, but now I see that wasn't the (only) problem. I ran the file with the configuration code you wrote, and LibreOffice gives Capture (Word displays something similar.) The "T" ought to be centered with respect to the entire line, not just with respect to the left-hand column. One way to construct equations with numbers in Microsoft Word is to make a one-row three-column table, with an empty left-hand column, the equation in the middle column, and the equation number set flush right in the last column. It seems that the configuration code so far sets up a one-row two-column format.

The answers in https://superuser.com/questions/594559/how-do-you-easily-add-equation-numbers-to-microsoft-word-2010-equations explain a newer way to have equation numbers in Word, using # to separate the equation from the equation number. That would not be hard to do manually on files of the size which I write; that is, I could use make4ht to get a DOCX file with misplaced equation numbers, then in Word, manually add the # character. But an automated solution would be better.

Your last point makes me concerned that the following, less "minimal" example will be impossible to convert, because the first equation in the alignment is not numbered but the second and third are.

\def\enum#1(#2:#3){\refstepcounter{equation} \label{#1#2}
              \tag{#1\theequation#3}}
\def\eoldnum#1(#2:#3){\tag*{$\mathrm{(\ref{#1#2}#3)}$}}

\begin{document}
See
\[ T  \enum A(shouldbeOne:) \]

\slshape
See
\begin{align*}
    x &=1  \\
  x+y &=2 \eoldnum A(shouldbeOne:a) \\
  z/2 &=3 \eoldnum A(shouldbeOne:'')
\end{align*}
\end{document}

The LaTeX output is Capture The "A" in the equation numbers would be for an Appendix. The last two equation numbers show two different ways of indicating a close relationship between an equation and a previous equation. The first way uses a lower-case letter, "a" in this case. Both letters in the full equation number should be upright, even if the surrounding environment is not upright, as is the case in this example. The second way of indicating a close relationship is to append a prime symbol (or, in the case of this example, two prime symbols). One has to avoid having such prime symbols misinterpreted as closing quotation marks, as in the last comment in https://tex.stackexchange.com/questions/426408/labeling-connecting-equations. This LaTeX code solves these problems as far as LaTeX is concerned while avoiding Plain TeX constructs (unlike my earlier version). But based on your comments, I fear it may not be possible to convert such structures into ODT format. (Right now, make4ht does not successfully convert them to ODT format.)

ThiloteE commented 2 years ago

Edit: This post by me was VERY wrong. Don't follow my advise.

hmmm try this:

z/2 &=3 \eoldnum A(shouldbeOne:\'\')

or

z/2 &=3 \eoldnum A(shouldbeOne:\a'\a')

Does one of the two work?

~~Explanation: \ in front of another symbol forces (La)Tex to render the symbol after \ literally. For '' This also should work in math-mode. For tabbing environment it has to be \a'.~~ Sorry, Latex is complicated 😥 I don't like it either. Unfortunately, it is what it is.

Source:

https://en.m.wikibooks.org/wiki/LaTeX/Special_Characters

gl-utah commented 2 years ago

Thanks for your interest, @ThiloteE! The LaTeX command I used, \def\eoldnum#1(#2:#3){\tag*{$\mathrm{(\ref{#1#2}#3)}$}}, creates the correct LaTeX output (pictured in my earlier posting) but running it through make4ht produces in LibreOffice the incorrect Capture. Your comment prompted me to experiment some more, and the alternative definition \def\eoldnum#1(#2:#3){\tag*{(\ref{#1#2}$\mathrm{#3}$)}} also gives the correct LaTeX output. I did not know that the "#1" and "#2" variables will automatically be upright even when the current text font is not upright; that is the behavior I want. The "#3" variable is either a letter, in which case it should be upright, or it should be one or more prime symbols; wrapping "#3" first in \mathrm{} and then in $ $ accomplishes that. Unfortunately, running the file with the new definition of \eoldnum through make4ht doesn't improve the ODT file, which LibreOffice shows as being unchanged.

I've not been able to figure out a way to get the same LaTeX output without switching to math mode or without using \mathrm. I thought about trying to avoid going into math mode by checking if "#3" was one or more ' symbols, and if so, specifying the prime character directly; but what I want is a superscripted prime character, and I think math mode is the only way to get that. If I leave out \mathrm, then in the example LaTeX output, the second equation number becomes the incorrect (A1 a ) [ignore the extra spaces] instead of the correct (A1a).

Changing the input text, rather than the macro definition, is recommended against in the first paragraph of https://www.kodymirus.cz/tex4ht-doc/Configurations.html#private-configuration-files: "It is highly recommended to leave source LaTeX and TeX files intact, and not introduce TeX4ht configurations there. The configurations should be introduced indirectly in private configuration files." Nevertheless, I tried your suggestions. However, \a did not print a literal "a" because \a is already defined to place a "ring" accent over the following letter, and \' printed neither a literal prime symbol nor a literal close quote because \' is already defined to place an acute accent over the following letter. These would seem to constitute counterexamples to: "\ in front of another symbol forces (La)Tex to render the symbol after \ literally." In addition, I'm not sure that what I really want is to get a "literal" character. When I type ' in an equation number, I want a prime superscript placed above and to the right of the equation number, not a close quote character (and not an acute accent placed over the equation number). When I type a in an equation number, I want a Roman a, but "literal" and "Roman" may not be the same thing.

The complications of LaTeX I've coped with, with uneven levels of success, for a long time, but the complications of ODT and DOCX and Microsoft Word's equation input formats are even harder for me. Thanks for your help!

ThiloteE commented 2 years ago

Oh no, i clearly sent you some wrong information. I apologize. I think i remembered something from outside math-mode, looked it up and did not look at the graphic properly. You are completely right, \ in mathmode does not render a symbol literally.

gl-utah commented 2 years ago

@michal-h21 , perhaps my minimal example was too minimal. It only showed the align* environment. AMSLaTeX also has the environments gather*, multline*, and alignat*. A more complete example follows. Again, if make4ht can't get the equation numbers in the right place, it would not be hard for me to manually edit the DOCX file to add # characters to move the equation numbers.

\documentclass{article}
\usepackage{amsmath}
\def\enum#1(#2:#3){\refstepcounter{equation} \label{#1#2}
              \tag{#1\theequation#3}}
\def\eoldnum#1(#2:#3){\tag*{$\mathrm{(\ref{#1#2}#3)}$}}
% this also would work:
%\def\eoldnum#1(#2:#3){\tag*{(\ref{#1#2}$\mathrm{#3}$)}}

\begin{document}
See
\[ T  \enum A(shouldbeOne:) \]
and the unnumbered
\[ U \,. \]

\slshape
Also see
\begin{align*}
  x &=1  \\
  x+y &=2 \eoldnum A(shouldbeOne:a) \\
  z/2 &=3 \eoldnum A(shouldbeOne:') \\
  w &= 4 \enum A(shouldbeTwo:)
\end{align*}
and
\begin{gather*}
  x=1 \\
  x+y=2 \eoldnum A(shouldbeOne:b) \\
  z/2 = 3 \eoldnum A(shouldbeOne:'') \\
  w = 4 \,.\enum A(shouldbeThree:)
\end{gather*}
\normalfont

A long numbered expression
\begin{multline*}
  x + x + x + x + x + x + x + x + x + x + x + x + x + x + {}\\
  x + x + x + x + x + x + x + x + x + x + x + x + x + x + {} \\
  x + x + x + x + x + x + x + x + x + x + x + x + x + x \enum A(shouldbeFour:)
\end{multline*}
and a long unnumbered one
\begin{multline*}
  x + x + x + x + x + x + x + x + x + x + x + x + x + x + {}\\
  x + x + x + x + x + x + x + x + x + x + x + x + x + x + {}\\
  x + x + x + x + x + x + x + x + x + x + x + x + x + x. 
\end{multline*}

Finally,
\begin{alignat*}{4}
  a &= b\qquad & a &= b\\
  c &= d & c &= d \eoldnum A(shouldbeOne:c)\\
  e &= f & e &= f \eoldnum A(shouldbeOne:''')\\
  g &= h & g &= h\,.\enum A(shouldbeFive:)
\end{alignat*}

\end{document}

LaTeX produces this output, which is correct: Capture make4ht produces an ODT file which LibreOffice displays as: Capture I use all of these environments (or their Plain TeX equivalents), but I use align* the most.

gl-utah commented 2 years ago

@ThiloteE, no problem! I really appreciate your effort & assistance.