Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to force equations to right of cell when using tableEqns? #343

Open
paul-kelleher opened this issue Feb 21, 2022 · 25 comments
Open

Comments

@paul-kelleher
Copy link

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!

@lierdakil
Copy link
Owner

Not entirely sure what you're trying to achieve. A picture could be helpful.

@paul-kelleher
Copy link
Author

Thanks. Pandoc-crossref w/ -MtableEqns works very nicely, but it center-justifies each equation and each number in their cells, as in this picture:

Screen Shot 2022-03-04 at 9 43 44 AM

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

Screen Shot 2022-03-04 at 9 44 07 AM

Thanks!

@lierdakil
Copy link
Owner

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?

@paul-kelleher
Copy link
Author

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.

@lierdakil
Copy link
Owner

Which Word version, specifically? 365? What OS?

@paul-kelleher
Copy link
Author

Which Word version, specifically? 365? What OS?

Word version 16.58 (via a 365 subscription) on MacOS 11.4 (Big Sur).

@lierdakil
Copy link
Owner

Thanks. I'll see what I can figure out.

@lierdakil
Copy link
Owner

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)

@lierdakil
Copy link
Owner

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.

@paul-kelleher
Copy link
Author

paul-kelleher commented Mar 5, 2022

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!

@paul-kelleher
Copy link
Author

I should add that only the tab stop solution seemed to work for me. But it worked, and I'm grateful!

@paul-kelleher
Copy link
Author

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:

Screen Shot 2022-03-14 at 3 30 45 PM

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:

Screen Shot 2022-03-14 at 3 30 56 PM

@llyu
Copy link

llyu commented Apr 27, 2022

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>

test.docx
test

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>

test_eqArr.docx
test_eqArr

I have recommended this solution to tomduck/pandoc-eqnos. I am wondering whether it is a good choice.

@lierdakil
Copy link
Owner

@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.

@joezhouchenye
Copy link

joezhouchenye commented Mar 16, 2023

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.

@paul-kelleher
Copy link
Author

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?

@joezhouchenye
Copy link

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:

image

After applying the macro:

image

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.

@paul-kelleher
Copy link
Author

paul-kelleher commented Jul 1, 2023

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?

@lierdakil
Copy link
Owner

I don't have good news 🤷 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).

@paul-kelleher
Copy link
Author

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!

@paul-kelleher
Copy link
Author

One thing that is curious is that all seems to works perfectly if I set tableEqns: false:
Screenshot 2023-07-01 at 9 01 01 AM

But if I change to tableEqns: true, I get this:
Screenshot 2023-07-01 at 9 01 34 AM

@joezhouchenye
Copy link

This is what I mean by the native word feature.

word-native

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:

  1. Set the pandoc-crossref options correctly
  2. Use pandoc to convert markdown to docx
  3. Use the Macro to fix equation formats and hyperlinks

@paul-kelleher
Copy link
Author

paul-kelleher commented Jul 1, 2023

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?

@paul-kelleher
Copy link
Author

paul-kelleher commented Jul 1, 2023

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!!

@paul-kelleher
Copy link
Author

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:

Screenshot 2023-12-11 at 11 13 00 AM

After running macro:

Screenshot 2023-12-11 at 11 06 22 AM

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants