pandoc-crossref
pandoc-crossref copied to clipboard
Any way to force equations to right of cell when using tableEqns?
Hello, thanks so much for pandoc-crossref. It's worked perfectly for me to achieve numbered equations and cross-references to them when compiling from md to docx (which was a need that led me to abandon LaTeX and begin writing in Markdown).
I was just wondering: is there any way to force equations to be right-justified within their respective cells when using the tableEqns feature?
Thanks!
Not entirely sure what you're trying to achieve. A picture could be helpful.
Thanks. Pandoc-crossref w/ -MtableEqns works very nicely, but it center-justifies each equation and each number in their cells, as in this picture:

I would love the ability to force both the equation and the number to be right-justified in their cell, as in this picture:

Thanks!
Hmm. The intention was for the cell with the equation number to be much narrower, so that the equation ends up being roughly centred. Something evidently goes wrong. AFAIU, you're targeting docx? What are you using to open the docx file?
If I understand correctly, the principal aim is to have the equation is in the horizontal centre of the page and the number is right-aligned, correct?
AFAIU, you're targeting docx? What are you using to open the docx file?
That's right, docx; and I'm using Word to open the docx file.
If I understand correctly, the principal aim is to have the equation is in the horizontal centre of the page and the number is right-aligned, correct?
That is correct, yes.
Which Word version, specifically? 365? What OS?
Which Word version, specifically? 365? What OS?
Word version 16.58 (via a 365 subscription) on MacOS 11.4 (Big Sur).
Thanks. I'll see what I can figure out.
The bad news is I wasn't able to reproduce the rendering you're getting, unfortunately I have neither macOS nor Office 365 readily accessible. Where I tested (web version of Word 365, LibreOffice, Word 2013 and 2019 on Windows), it seemed to render more or less as expected.
The good news is, I've made the tableEqns
layout configurable. See here. This is as of yet unreleased, but you can grab a macOS binary from https://github.com/lierdakil/pandoc-crossref/releases/download/nightlies/pandoc-crossref-master-macOS-20220305-a359a9.tar.xz
NB: I think I might have messed up vertical alignment of equation numbers in tables, I'll see to fixing it later.
Right-aligning can be done with the following metadata for example:
tableEqns: true
eqnBlockTemplate: |
--------------------------------------------------------------- -------
$$t$$ $$i$$
--------------------------------------------------------------- -------
eqnBlockInlineMath: true
(the extra newline in the table is important!)
You need eqnBlockInlineMath: true
here so that pandoc doesn't force center alignment on math inside the table (as it unconditionally does for display math; the rendering shouldn't be otherwise affected, as Word treats any math that's alone in a paragraph as display math).
If you don't mind tinkering a bit, I believe the option of using tab stops to align equations should work the best in general, e.g.
tableEqns: true
eqnBlockTemplate: |
`<w:pPr><w:tabs><w:tab w:val="center" w:pos="4680" /><w:tab w:val="right" w:pos="9360" /></w:tabs></w:pPr><w:r><w:tab /></w:r>`{=openxml} $$t$$ `<w:r><w:tab /></w:r>`{=openxml} $$i$$
eqnBlockInlineMath: true
You'd have to manually specify tab stop positions, though (the values here, 4680
and 9360
, are appropriate for A4 with 1-inch margins)
Ah, right, vertical alignment can be fixed with
eqnBlockTemplate: |
+---------------------------------------------------------------:+-----:+
| ```{=openxml} | ```{=openxml}
| <w:tcPr><w:vAlign w:val="center"/></w:tcPr> | <w:tcPr><w:vAlign w:val="center"/></w:tcPr>
| ``` | ```
| $$t$$ | $$i$$
+----------------------------------------------------------------+-----+
Not pretty, but seems to work.
I'd originally posted here that it didn't work, but I made a mistake: I wasn't using the new binary! I changed to using it, and then all worked great!
Pandoc-crossref has been a lifesaver for me and I really appreciate your work on it!
I should add that only the tab stop solution seemed to work for me. But it worked, and I'm grateful!
If you don't mind tinkering a bit, I believe the option of using tab stops to align equations should work the best in general
As I mentioned above, this was the only solution that worked for me. But I've just noticed that this solution forces the equation to be displayed "in line" by which I mean, for example, that the limits above and below summation notation are set to the right of the summation sign, like this:
If I change the display manually to fix the placement of notation above the summation, it forces the equation number below the equation, like this:
In the '*.docx' file, the right-aligned equation numbering would look better using <m:eqArr>.
Here are sample files to show the difference in displaying the equation number. The equation part in the origin file is:
<m:oMath>
<m:r><m:t>y=f(x) (1)</m:t></m:r>
</m:oMath>
And, the equation part using the <m:eqArr> modifier:
<m:oMath>
<m:eqArr>
<m:eqArrPr><m:maxDist m:val="1"/></m:eqArrPr>
<m:e>
<m:r><m:t>y=f(x)#(1)</m:t></m:r>
</m:e>
</m:eqArr>
</m:oMath>
I have recommended this solution to tomduck/pandoc-eqnos. I am wondering whether it is a good choice.
@llyu, unfortunately, unless we're willing to parse LaTeX ourselves and convert it to OOXML (which is very involved), there's no way to produce m:eqArr
in the output. Pandoc uses texmath library, but texmath, while it does parse m:eqArr
, doesn't output m:eqArr
. Feel free to create an issue upstream if you think there's a way to make it work with eqArr
.
I found a nearly perfect solution to format equations in a .docx file.
First, set the following options when output
equationNumberTeX: \\#
eqnIndexTemplate: ($$i$$)
Second, in the generated .dox file, use this macro:
Sub EquationBookmark()
' Convert equations to professional format
ActiveDocument.OMaths.BuildUp
' Add Bookmarks to equations
' eq:# style is not supported
' eq# is used
Dim eq As OMath
Dim i As Long
Dim s As String
i = 1
For Each eq In ActiveDocument.OMaths
If eq.Type <> wdOMathInline Then
s = "eq" & CStr(i)
With ActiveDocument.Bookmarks
.Add Range:=eq.Range, Name:=s
.DefaultSorting = wdSortByLocation
.ShowHidden = False
End With
i = i + 1
End If
Next eq
' Auto-generated eq:# hyperlinks
' must be converted to eq#
Dim h As Hyperlink
For Each h In ActiveDocument.Hyperlinks
If InStr(1, h.SubAddress, "eq:") = 1 Then
Dim Rng As Range
Dim StrAddr As String, StrSubAddr As String
Dim SStrTxt As String
With h
Set Rng = .Range
StrAddr = .Address
StrSubAddr = .SubAddress
StrTxt = .TextToDisplay
.Delete
End With
StrSubAddr = Replace(StrSubAddr, "eq:", "eq")
ActiveDocument.Hyperlinks.Add Anchor:=Rng, Address:=StrAddr, SubAddress:=StrSubAddr
i = i + 1
End If
Next h
End Sub
Now, everything will be perfect.
I use Microsoft Word's native #(number) feature to add equation numbers, but equations must be in professional mode.
Auto-generated bookmarks will be lost after conversion.
I have some issues with the bookmarks at first. Not sure, how pandoc added eq:#
style bookmarks, but Microsoft Word won't allow you to add such style bookmarks.
So, I add eq#
style bookmarks first and convert all hyperlinks to this style.
Does this solution give you equation numbers on the right while allowing equations to be displayed in full "Display" mode, so that (for example) notation can be placed above and below a sigma summation symbol, rather than inline to the right of it?
Yes, it does. It just uses Microsoft Word's native support for equation numbers (I am using 365) which works quite well.
You can try it by inserting an equation in Word, adding #(equation number)
at the end, and typing an Enter
.
Converting to professional mode is an alternative to typing an Enter
.
Before applying the macro:
After applying the macro:
The Macro above has some problems with inline maths, as it will add bookmarks to inline equations as well.
I will update it to omit inline equations.
Hi again! I'm sorry, but I'm still trying to navigate the issue I raised up above here. When using pandoc-crossref to output to a docx file, I can align the equation numbers to the right nicely with tableEqns: true
, but the equation that results is what I call in that post "inline" rather than "Display". If I change to "Display" manually to fix the placement of notation above the summation, it forces the equation number below the equation (again as illustrated here). Can I ask again whether/how I might have all equations in the "Display" format while still having the equation number placed all the way on the right?
I don't have good news :shrug: If the clever hack suggested by @joezhouchenye doesn't work for you, I can't suggest anything else. I've tried to understand how Word handles eqArr
to maybe hack it into Pandoc's texmath, but frankly I'm stumped, no XML I wrote manually gives the results we would want here (although I was testing in the online version of Word, maybe it's different in the desktop version, but I don't have it on hand).
Thanks. I wonder if @joezhouchenye would be able to walk me through how to use his macro? @joezhouchenye, when you write "I use Microsoft Word's native #(number) feature to add equation numbers," does that mean that I can't just apply the macro to the docx document that is produced by pandoc using pandoc-crossref? Thanks!
One thing that is curious is that all seems to works perfectly if I set tableEqns: false
:
But if I change to tableEqns: true
, I get this:
This is what I mean by the native word feature.
The equation must be in the syntax like a=b+c #(1)
. Microsoft Word can convert such syntax to the format you want. Type an Enter at the end or convert the equation to professional format as shown in the GIF.
The Macro does such conversion automatically and fixes the equation hyperlinks.
To use this Macro, pandoc and pandoc-crossref need to convert markdown to the above syntax required.
Luckily, pandoc-crossref offers two options equationNumberTeX
and eqnIndexTemplate
for us to achieve this.
So, the basic steps are:
- Set the pandoc-crossref options correctly
- Use pandoc to convert markdown to docx
- Use the Macro to fix equation formats and hyperlinks
Got it, thanks for that walk-through @joezhouchenye! I will try to implement this. Do you know if you ended up updating the macro to play nice with inline math?
I just got this to work and all I can say is THANK YOU, @joezhouchenye!!!! I haven't check through the document to see how it does with inline math. Will report back!
UPDATE: I can't see any issues with inline math. Hooray!!
Hi again @joezhouchenye: This method has been working wonderfully for me, so thanks again! But I've just run into one glitch (or at least I think it's a glitch). When I run the macro, fractions that have skewed fractions as their numerator and/or denominator are being changed so that those skewed fractions become stacked fractions. Here's an example:
Before running macro:
After running macro:
I can change these back by highlighting each fraction and then selecting "Change to Skewed Fraction", but it'd be great if there were a way to ensure the macro doesn't change the fraction.
Any thoughts?