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

How do I prevent asciidoctor-multipage from resetting the latex equation counter after every HTML Page? #39

Open
SamMachariaPhD opened this issue Nov 12, 2022 · 3 comments

Comments

@SamMachariaPhD
Copy link

SamMachariaPhD commented Nov 12, 2022

I'm testing the asciidoctor-multipage extension and want to generate asciidoctor HTML multipages. I'm also interested in PDF and one-page HTML.
I made the following test documents attached here ["test.adoc", "chap1.adoc", "chap2.adoc", "chap3.adoc"].

test.adoc

= Title: Subtitle   
:author: Sam 
:email: test-email@emails.mail
:description: asciidoctor-multipage  
:keywords: Asciidoctor, Multipage 
:doctype: book
:backend: docbook
:stem: latexmath 
:figure-caption: Figure
:figure-number: 
:source-highlighter: highlight.js
:numbered:
:eqnums: Equation
:imagesdir: images
:sectnums: 

This document contains all 3 chapters. 

include::chap1.adoc[] 

include::chap2.adoc[] 

include::chap3.adoc[] 

*** 

chap1.adoc

== Chapter 1 

This is my chapter one. 

[stem#eqn-normalStress,reftext='{eqnums} {counter:eqnum}'] 
++++
\begin{align}
\sigma\ (\text{stress}) = \frac{F\ (\text{load})}{A\ (\text{area})}\ \frac{N}{m^2}
\end{align}
++++

From <<#eqn-normalStress>>, you can calculate the normal stress. 

chap2.adoc

== Chapter 2 

This is a test chapter 2. 

[stem#eqn-normalStrain,reftext='{eqnums} {counter:eqnum}'] 
++++
\begin{align}
\epsilon\ (\text{strain}) = \frac{\Delta L\ (\text{change in length})}{L\ (\text{original length})}
\end{align}
++++

From <<#eqn-normalStrain>>, you can calculate strain. 

chap3.adoc

== Chapter 3 

This is the final test chapter. 

[stem#eqn-twistShear,reftext='{eqnums} {counter:eqnum}'] 
++++
\begin{align}
\text{Shear strain, } \gamma = \frac{R\theta}{L} 
\end{align}
++++

Calculate shear strain from <<#eqn-twistShear>>.

Terminal commands

Having installed the required extension packages on Ubuntu 20.04, I used the following commands to generate the outputs in various directories.

$ asciidoctor --backend html5 -D htmlDir -a data-uri test.adoc
$ asciidoctor -r asciidoctor-multipage -D htmlsDir --backend multipage_html5 -a data-uri test.adoc
$ asciidoctor -r asciidoctor-pdf -r asciidoctor-mathematical -D pdfDir -b pdf test.adoc

All documents are generated successfully, except that, unlike in the PDF and the one-page HTML output, the asciidoctor-multipage HTMLs output seems to reset or forget the latex equation counter on every page. Every equation is labelled (1) even when the cross-reference correctly indicates Equation 3.

I would like asciidoctor-multipage to remember the counter and match all equation labels as in (1) (2) (3) on various pages. Not (1) (1) (1). How do I force the asciidoctor-multipage extension to preserve the latex equation numbering similar to the PDF and the one-page HTML?

Thank you.
Regards, Sam.

@SamMachariaPhD SamMachariaPhD changed the title How to make the asciidoctor-multipage extension preserve the consistent LaTex equation numbering in various HTML outputs How do I prevent asciidoctor-multipage from resetting the latex equation counter after every HTML Page? Nov 13, 2022
@owenh000
Copy link
Owner

It looks like the equation numbers are added by the MathJax script when it runs during page load, and there is no numbering added by Asciidoctor that could be used to correctly number the equations. See this excerpt, the stem block, from the _chapter_2.html output:

<div id="eqn-normalStrain" class="stemblock">
<div class="content">
\[\begin{align}
\epsilon\ (\text{strain}) = \frac{\Delta L\ (\text{change in length})}{L\ (\text{original length})}
\end{align}\]
</div>
</div>

The MathJax script adds the equation numbers as configured in this excerpt, later in _chapter_2.html:

MathJax.Hub.Config({
  TeX: { equationNumbers: { autoNumber: "Equation" } }
})

As you can see, only Asciidoctor knows the reftext of the stem block. MathJax only knows the numbering prefix (and in this case apparently doesn't use it), but not the actual number. So it seems to me like it is not possible to fix this without changes to Asciidoctor. I'll leave this issue open here for now in case someone has opportunity to work on it.

@SamMachariaPhD
Copy link
Author

SamMachariaPhD commented Nov 14, 2022

Thank you for your response @owenh000
Meanwhile, I decided to use the LaTex \tag{}. I also made a simple Python code to update an equation's tag because I did not want to update each tag manually. This may be a crude method, but it served the purpose.

Updated: chap1.adoc

== Chapter 1 

This is my chapter one. 

[stem#eqn-normalStress,reftext='{eqnums} {counter:eqnum}'] 
++++
\begin{align}\tag{#eqnum#}
\sigma\ (\text{stress}) = \frac{F\ (\text{load})}{A\ (\text{area})}\ \frac{N}{m^2}
\end{align}
++++

From <<#eqn-normalStress>>, you can calculate the normal stress. 

Updated: chap2.adoc

== Chapter 2 

This is a test chapter 2. 

[stem#eqn-normalStrain,reftext='{eqnums} {counter:eqnum}'] 
++++
\begin{align}\tag{#eqnum#}
\epsilon\ (\text{strain}) = \frac{\Delta L\ (\text{change in length})}{L\ (\text{original length})}
\end{align}
++++

From <<#eqn-normalStrain>>, you can calculate strain. 

Updated: chap3.adoc

== Chapter 3 

This is the final test chapter. 

[stem#eqn-twistShear,reftext='{eqnums} {counter:eqnum}'] 
++++
\begin{align}\tag{#eqnum#}
\text{Shear strain, } \gamma = \frac{R\theta}{L} 
\end{align}
++++

Calculate shear strain from <<#eqn-twistShear>>.

Python Code to Update \tag{#eqnum#}

main_file = "test.adoc"
start_key_word = "include::"
end_key_word = ".adoc["
eqn_tag = "#eqnum#"
eqn_count = 1

bool_write_line = False
file_names = [main_file[:-5]] # remove .adoc extension

############get all file names as included###############

with open(main_file) as infile:
    for line in infile:
        if line.startswith(start_key_word):
            bool_write_line = end_key_word in line
        if bool_write_line:
            line = line.split(end_key_word)[0]
            line = line.split(start_key_word)[1]
            file_names.append(line)
            bool_write_line = False

############update equation tags###############

for i in range(len(file_names)):
    file_path = file_names[i]+".adoc"

    infile = open(file_path, 'r')
    lines =  infile.readlines()

    for i in range(len(lines)):
        if eqn_tag in lines[i]:
            #print(lines[i]) # original tag
            nl = lines[i].replace(eqn_tag, str(eqn_count))
            lines[i] = nl
            #print(lines[i]) # replaced tag 
            eqn_count += 1

    with open(file_path, 'w') as filehandle:
        for listitem in lines:
            filehandle.write(f'{listitem}')

@owenh000
Copy link
Owner

owenh000 commented Nov 17, 2022

Hi Sam, thanks for sharing your workaround. It might be possible to improve this behavior by doing the following in asciidoctor-multipage: (1) disabling MathJax auto-numbering and (2) adding an explicit Latex tag{} in each case. Since there isn't (as far as I know) an "equation number" in Asciidoctor, it would use the reftext value as the tag. But it might be too fragile, require changes to Asciidoctor, and/or break with unrelated upstream changes.

Note :eqnums: only takes three values; see the Document Attributes Reference. It looks like it just gets reset to AMS.

Edit: removed a stray parenthesis

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

2 participants