mbutterick / pollen-users

please use https://forums.matthewbutterick.com/c/typesetting/ instead
https://forums.matthewbutterick.com/c/typesetting/
52 stars 0 forks source link

questions on MathJax #25

Open jtangpanitanon opened 4 years ago

jtangpanitanon commented 4 years ago

Hi, I'm a new pollen & racket users. I am experimenting with the source code for "Typography for Laywers" and trying to add LaTex equations into it on my computer.

Here is what I added into my html.pp file

◊(define ($ . xs) (mathjax ,(apply string-append ("$" ,@xs "$")))) ◊(define ($$ . xs) (mathjax ,(apply string-append `("$$" ,@xs "$$"))))

◊(define-meta title "where do the rules come from?") ◊hanging-topic[(topic-from-metas metas)]{Professional typography}

Example 1: ◊h1{◊${\mathcal{O} = 2_{\alpha+1}}} Example 2: ◊${|\psi\rangle = \sum_nc_n|n\rangle} `

However, the latex code only appears correctly in Example 1 where '◊h1' is used but this creates a bigger font than what I need. How can I make the latex code works without '◊h1'?

Another question is how do I move the following lines to pollen.rkt file? This is not obvious to me in the tutorial.

◊(define ($ . xs) (mathjax ,(apply string-append ("$" ,@xs "$")))) ◊(define ($$ . xs) (mathjax ,(apply string-append `("$$" ,@xs "$$"))))

Thanks!

sorawee commented 4 years ago

Welcome to Pollen and Racket!

FYI, in Github issues, which use Markdown, you should use triple backtick when you want to show a fragment of code so that the code is displayed verbatim and not parsed as Markdown.

For your second question, put:

(define ($ . xs)
  `(mathjax ,(apply string-append `("$" ,@xs "$"))))
(define ($$ . xs)
  `(mathjax ,(apply string-append `("$$" ,@xs "$$"))))

in pollen.rkt. Make sure that your pollen.rkt either has (provide (all-defined-out)) or (provide $ $$) (you probably want the former).

What happens to Example 2? Can you show me the output?

sorawee commented 4 years ago

This is a minimal example of what you are trying to do. It seems to work fine for me.

Screen Shot 2019-12-31 at 19 40 14

Note that I have three files in the project directory: pollen.rkt, template.html.p, and test.html.pm. In Pollen, the file extension is important. Not sure if the problem that you experienced is related to the fact that you name your file html.pp.

jtangpanitanon commented 4 years ago

Hi @sorawee , thanks for your nice answer. I tried to copy your solution, it kind of works but is slightly different than yours. Any ideas what went wrong?

Screen Shot 2020-01-09 at 2 32 28 PM ``` #lang pollen ◊(define-meta title "Hello World!") Example 1: ◊h1{◊${\mathcal{O} = 2_{\alpha+1}}} Example 2: ◊${|\psi\rangle = \sum_nc_n|n\rangle} ``` ``` #lang racket (provide (all-defined-out)) (define ($ . xs) `(mathjax ,(apply string-append `("$" ,@xs "$")))) (define ($$ . xs) `(mathjax ,(apply string-append `("$$" ,@xs "$$")))) ``` ``` ◊(select-form-metas 'title here)

◊(select-from-metas 'title here)

◊(->html doc) ```
jtangpanitanon commented 4 years ago

Also, another problem is when I try to add your suggestion in in the template for "Typography for Laywers"

#lang pollen/mode racket/base
(require "scribblings/pollen-rkt.scrbl" pollen/tag)
(provide (all-from-out "scribblings/pollen-rkt.scrbl"))

(module setup racket/base
  (provide (all-defined-out)) ;; <- don't forget this line in your config submodule!
  (require pollen/setup racket/path)
  (define (omitted-path? p) (path-has-extension? p #".sh")))

(provide ie-payment-warning)
(define (ie-payment-warning)
  (define div (default-tag-function 'div))
  (define p (default-tag-function 'p))
  (define strong (default-tag-function 'strong))
  ◊div[#:class "ie reader-note"]{◊p{Because of security considerations, my payment links ◊strong{do not support Internet Explorer 11 or earlier}. Please use a different browser.}})

(define ($ . xs)
`(mathjax ,(apply string-append `("$" ,@xs "$"))))
(define ($$ . xs)
`(mathjax ,(apply string-append `("$$" ,@xs "$$"))))

with the following html.pm file

#lang pollen

◊(define-meta title "where do the rules come from?")
◊hanging-topic[(topic-from-metas metas)]{Professional typography}

Example 1: ◊h1{◊${\mathcal{O} = 2_{\alpha+1}}}
Example 2: ◊${|\psi\rangle = \sum_nc_n|n\rangle}

I get the following

Screen Shot 2020-01-09 at 3 13 41 PM
sorawee commented 4 years ago

Let me address your first comment first. That is not a Pollen issue. Different browser has different initial CSS style settings. I used Chrome when I took the screenshot above. If you use a different browser (e.g., Safari), that would explain the discrepancy.

FWIW, there are several CSS libraries that attempt to give a better default settings so that all browsers have the same appearance. For example:

So I recommend loading one of these in template.html.p (in the <head> section) to make browsers appear in the same way.

sorawee commented 4 years ago

For your second comment: did you add

<script type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$']]}});
</script>

to your template.html.p?

jtangpanitanon commented 4 years ago

For your second comment: did you add

<script type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$']]}});
</script>

to your template.html.p?

Yep this is my template.html.p

<!DOCTYPE html>
◊(define parent-page (parent here))
◊(define previous-page (previous here))
◊(define next-page (next here))
◊(define here-title (or (select-from-metas 'title here) (symbol->string here)))
◊(define toolbar? (not (select-from-metas 'toolbar-blank metas)))

◊(define (make-side-nav id url text)
  ◊div[#:class "nav-outer" #:id id]{◊(link (or url "") ◊div[#:class "nav-inner"]{◊div[#:class "nav-flex" text]})})
◊(define center-cell-width 14)
◊(define side-cell-width (/ (- 100 (+ 10 (* center-cell-width 2))) 2))
◊(local-require pollen/tag)
◊; the name `link` is already defined as a function that makes hyperlinks,
◊; so we use `make-default-tag-function` to make a literal `link` tag
◊(define literal-link (make-default-tag-function 'link)) 
◊(define (make-subnav children)
  (apply ul #:class "subnav"
    (for/list ([child (in-list children)])
      (li (xref (select-from-metas 'title child))))))

◊(local-require css-tools)

<head>
<script type="text/javascript"
  src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$']]}});
</script>
<title>◊(select-form-metas 'title here)</title>

  <meta charset="UTF-8">
    <script type="text/javascript">
if (navigator.appVersion.indexOf("Win")!=-1) {
    ◊; got windows
    document.write('<link rel="stylesheet" type="text/css" media="all" href="fonts/equity-a.css" />');
} else if (navigator.appVersion.indexOf("Mac")!=-1) {
    if (navigator.userAgent.match(/iPad/i) != null) {
        ◊; got ipad
        ◊; style sheet for ipad 2
        document.write('<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="fonts/equity-b.css" type="text/css" />');
        ◊; style sheet for ipad 3
        document.write('<link rel="stylesheet" media="only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 2)" type="text/css" href="fonts/equity-a.css" />');
    } else {
        ◊; got mac
        document.write('<link rel="stylesheet" type="text/css" media="all" href="fonts/equity-b.css" />');
    }
} else {
    ◊; got something else
    document.write('<link rel="stylesheet" type="text/css" media="all" href="fonts/equity-a.css" />');
}

</script>

... 
sorawee commented 4 years ago

I believe the issue is that you didn't (provide (all-defined-out)) in pollen.rkt. If you don't want to provide everything that you define in that file, you can also just (provide $ $$) to selectively provide the two tags that you created.

jtangpanitanon commented 4 years ago

I believe the issue is that you didn't (provide (all-defined-out)) in pollen.rkt. If you don't want to provide everything that you define in that file, you can also just (provide $ $$) to selectively provide the two tags that you created.

Thanks @sorawee . It seems to solve half of the problem :)

Screen Shot 2020-01-09 at 4 08 47 PM

#lang pollen/mode racket/base

(provide (all-defined-out))

(require "scribblings/pollen-rkt.scrbl" pollen/tag)
(provide (all-from-out "scribblings/pollen-rkt.scrbl"))

(module setup racket/base
  (provide (all-defined-out)) ;; <- don't forget this line in your config submodule!
  (require pollen/setup racket/path)
  (define (omitted-path? p) (path-has-extension? p #".sh")))

(provide ie-payment-warning)
(define (ie-payment-warning)
  (define div (default-tag-function 'div))
  (define p (default-tag-function 'p))
  (define strong (default-tag-function 'strong))
  ◊div[#:class "ie reader-note"]{◊p{Because of security considerations, my payment links ◊strong{do not support Internet Explorer 11 or earlier}. Please use a different browser.}})

(define ($ . xs)
`(mathjax ,(apply string-append `("$" ,@xs "$"))))
(define ($$ . xs)
`(mathjax ,(apply string-append `("$$" ,@xs "$$"))))
sorawee commented 4 years ago

The problem is that Typography for Lawyers (henceforth TfL) has a lot of black magic that you really don't want. In this case, the problem is caused by the automatic hyphenation, which attempts to hyphenate your LaTeX code because it doesn't know that your code is not a prose that should not be tampered with. One way to fix the problem is to add mathjax to the list of tags on this line in pollen-rkt.scrbl to make the automatic hyphenation skips LaTeX code.

But again, you really don't want to base your project on TfL. TfL is in fact a quite unconventional Pollen project, e.g., using literate programming instead of writing code as usual. You probably won't need any of these features.

jtangpanitanon commented 4 years ago

Wow it works like magic. Thanks @sorawee ! I just found TfL template is quite beautiful and as I don't have HTML background, I won't be able to design things from scratch.