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

Update render inline code #227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

choeppler
Copy link
Contributor

@choeppler choeppler commented Oct 2, 2024

Currently \verb is used to render inline code. However, that has some downsides as it does not work in moving arguments or within macros like \multicolumn, and it doesn't break long inline code lines. This PR changes the implementation to use \texttt, which fixes those issues.

The PR provides three patches which can be integration tested with the following script to create test cases as described below:

#!/bin/env python3

import mistletoe
from mistletoe.latex_renderer import LaTeXRenderer

def render(f):
   with open(f, 'r') as fp:
     return mistletoe.markdown(fp, LaTeXRenderer)

# test case that fails for main but is fixed by 866bc2f35a95f18536a1626ec3f4d42b1209b939
test_0 = """
~~strikethrough~~
"""
# test case that fails for main but is fixed by 1fba4806a682d3edd0eb0dbe9e103784ac7e94f4
test_1 = """
this is the set of LaTeX escape chars: $ # { } & _ % \\ ~ ^
"""
# test case to check whether 4a97c287f0192f968c107d3cf428e78d4804e1d7 still renders inline code as expected
test_2 = """
line `inline code` line

line `inline  code with two spaces` line

line `inline\tcode with tab` line

line `inline\t\tcode with two tabs` line

line `inline  code with\nnewline` line 

line without inline code which would require a linebreak because it is simply way too long to fit.

line `inline code which would require a linebreak because it is simply way too long to fit`.

this is the set of LaTeX escape chars in inline code: `$ # { } & _ % \\ ~ ^`

"""
for test, name in zip([test_0, test_1, test_2], ['test_0', 'test_1', 'test_2']):
  open('%s.md'%name, 'w').write(test)
  open('%s.tex'%name, 'w').write(render('%s.md'%name))
  • for main pdflatex test_0.tex results in the following error: ! LaTeX Error: Unknown option 'normalem' for package 'ulem'. With 866bc2f it works as expected

  • for main pdflatex test_1.tex results in another error:

    ! Missing $ inserted.
    <inserted text> 
                  $
    l.4 ...eX escape chars: \$ \# \{ \} \& \_ \% \ ~ ^
    

    With 1fba480 it works as expected

  • For 1fba480 pdflatex test_2.tex shows the behavior of the original implementation of inline code rendering:

    image

    Finally, this PR's main contribution is the changed implementation of inline code rendering in 4a97c28 the result of which can be seen here:

    image

@coveralls
Copy link

coveralls commented Oct 2, 2024

Coverage Status

coverage: 94.201% (-0.006%) from 94.207%
when pulling 4a97c28 on boschresearch:fix-render-inline-code
into a40cd2f on miyuchina:master.

@choeppler choeppler force-pushed the fix-render-inline-code branch 2 times, most recently from ee2ab63 to d023588 Compare October 2, 2024 12:36
Rendering package options with string delimiters leads to LaTeX errors,
e.g., when `render_strikethrough` activates the 'ulem' package with
option 'normalem', the rendered LaTeX should read
`\usepackage[normalem]{ulem}` rather than
`\usepackage['normalem']{ulem}` as it previously did. This patch fixes
that.

NB: Too keep the implementation simple (and as it has not shown adverse
side-effects when using pdflatex), the generated LaTeX for an added
package listings without options is now \usepackage[]{listings} rather
than \usepackage{listings}.
Escape some additional characters in raw text which have special meaning
in LaTeX and which previously hadn't been dealt with by
`latex-renderer`'s `render_raw_text`-method.

Refactor the corresponding test case along the lines of the other
parameterized test cases to improve the code's consistency.
Currently `\verb` is used to render inline code. However, that has some
downsides as it does not work in moving arguments or within macros like
\multicolumn, and it doesn't break long inline code lines. This patch
changes the implementation to use `\texttt`, which fixes those issues.
@choeppler choeppler force-pushed the fix-render-inline-code branch from d023588 to 4a97c28 Compare October 2, 2024 12:46
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

Successfully merging this pull request may close these issues.

2 participants