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

[bug] when lstset{mathescape=true}, lstinline cannot work well #4716

Open
bubifengyun opened this issue Jun 19, 2018 · 17 comments
Open

[bug] when lstset{mathescape=true}, lstinline cannot work well #4716

bubifengyun opened this issue Jun 19, 2018 · 17 comments

Comments

@bubifengyun
Copy link

@bubifengyun bubifengyun commented Jun 19, 2018

Watch source https://github.com/jgm/pandoc/blob/master/src/Text/Pandoc/Writers/LaTeX.hs?#L1048#L1053

when

\lstset{mathescape=true}

code in two $ should not be converted, such as
$x_1$ should be

\passthrough{\lstinline!$x_1$!}

but in fact it is,

\passthrough{\lstinline!$x\_1$!}

I think, when in mathescape=true, it should not convert the strings between two $s.

#4713

https://stackoverflow.com/questions/50898498/how-to-insert-in-lstinline/50919878#50919878

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Jun 19, 2018

Sorry, I don't know hs language, I don't know how to edit source directly.

@mb21
Copy link
Collaborator

@mb21 mb21 commented Jun 19, 2018

Thank you for submitting a bug report. However, we cannot improve pandoc or help you unless you give us all of the following information:

  • the pandoc version (check using pandoc -v and try to reproduce the bug with the latest released version of pandoc, or even better: the development version or nightly build.)
  • the exact command line used
  • the exact input used
  • the output received
  • the output you expected instead

A small test case (just a few lines) is ideal. If your input is large, try to whittle it down to a minimum working example.

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Jun 19, 2018

I had pointed the source code, pandoc version 2.2.1. in the source code, it forget to process when mathescape = true. so every special string would be converted. but string between $ and $, as math express should not be converted. I will make a small bug code. sorry.

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Jun 20, 2018

pandoc

pandoc 2.2.1
Compiled with pandoc-types 1.17.4.2, texmath 0.11, skylighting 0.7.0.2
Default user data directory: /home/litianci/.pandoc
Copyright (C) 2006-2018 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

exact command

$ /usr/bin/pandoc +RTS -K512m -RTS test.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output result.tex --table-of-contents --toc-depth 2 --template template.tex --number-sections --highlight-style tango --pdf-engine xelatex --biblatex --listings --top-level-division=chapter --variable tables=yes --standalone
$ xelatex result.tex


 you can see result.pdf

all the file all in folder i had submit.

.
├── result.pdf
├── result.tex
├── template.tex
└── test.md

ps:

  • test.md is input file
  • template.tex is template
  • result.tex is result of pandoc
  • result.pdf was created by xelatex

all related words was speak in result.pdf

pandoc_lstinline_bug.zip

bug source in https://github.com/jgm/pandoc/blob/master/src/Text/Pandoc/Writers/LaTeX.hs?#L1048#L1053

@mb21
Copy link
Collaborator

@mb21 mb21 commented Jun 20, 2018

I still don't quite understand, since you provide a lot of information, but the example is neither minimal (best would be a simple echo 'my input' | pandoc -t latex ) nor do you specify the expected output... but maybe jgm can figure out what you're getting at, since he wrote the LaTeX writer.

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Jun 20, 2018

@jgm I think it is simple enough, even the source code, I had already circled.

before complete escape, take if it is mathescape, then can take convert in double $?

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Jun 22, 2018

I upload a simple example, Thank you.

latex_lstinline.zip

I hope output like this.
_ _20180622231115

bubifengyun referenced this issue in dotlambda/pandoc Jun 22, 2018
Previously the LaTeX writer created invalid LaTeX
when `--listings` was specified and a code span occured
inside emphasis or another construction.

This is because the characters `%{}\` must be escaped
in lstinline when the listinline occurs in another
command, otherwise they must not be escaped.

To deal with this, adoping Michael Kofler's suggestion,
we always wrap lstinline in a dummy command `\passthrough`,
now defined in the default template if `--listings` is
specified.  This way we can consistently escape the
special characters.

Closes jgm#1629.
@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Jun 22, 2018

I had ref to dotlambda@730796e#comments I think the person who add this feature can understand this bug. Thank you.

@jgm
Copy link
Owner

@jgm jgm commented Sep 21, 2018

The LaTeX writer doesn't have any idea what is in the template and (with a minor exception) doesn't adjust its behavior based on that. It definitely doesn't parse the template. Moreover, there is currently no setting corresponding to mathescape=true.

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Sep 21, 2018

I had contacted with the listings package author, here is their reply.

Re: how about add lstget in listings package
Hoffmann, Jobst于2018年9月21日 星期五 下午21:01发送给bubifengyun...查看2个附件
详细信息
关闭按钮
Hi,
Am Freitag, den 21.09.2018, 12:39 +0800 schrieb bubifengyun@sina.com:
> I find that some time, I need to get some state of listings, to produ
> ce some code, like in pandoc. 
> 
> https://github.com/jgm/pandoc/issues/4716
> 
> if there are some code like \lstget{mathescape}, it would be easy to 
> solve such problem. thank you.
> 来自新浪邮箱触屏版
the problem ist, that there is no generic "true" or "false", so you
cannot do something like
if \lstget{mathescape} a \else b \fi
but you have to write
\makeatletter
\lst@ifmathescape mathescape is true\else mathescape is false.\fi
\makeatother
I attach a small example as source and as formatted text.
Kind regards
J. Hoffmann
-- 
Prof. Dr. Jobst Hoffmann Tel: +49 (241) 6009-5 31 59
Fachhochschule Aachen Abt. Jülich Fax: +49 (241) 6009-5 31 89
Fachbereich 09 email: j.hoffmann@fh-aachen.de

and there two files.

3 similar comments
@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Sep 21, 2018

I had contacted with the listings package author, here is their reply.

Re: how about add lstget in listings package
Hoffmann, Jobst于2018年9月21日 星期五 下午21:01发送给bubifengyun...查看2个附件
详细信息
关闭按钮
Hi,
Am Freitag, den 21.09.2018, 12:39 +0800 schrieb bubifengyun@sina.com:
> I find that some time, I need to get some state of listings, to produ
> ce some code, like in pandoc. 
> 
> https://github.com/jgm/pandoc/issues/4716
> 
> if there are some code like \lstget{mathescape}, it would be easy to 
> solve such problem. thank you.
> 来自新浪邮箱触屏版
the problem ist, that there is no generic "true" or "false", so you
cannot do something like
if \lstget{mathescape} a \else b \fi
but you have to write
\makeatletter
\lst@ifmathescape mathescape is true\else mathescape is false.\fi
\makeatother
I attach a small example as source and as formatted text.
Kind regards
J. Hoffmann
-- 
Prof. Dr. Jobst Hoffmann Tel: +49 (241) 6009-5 31 59
Fachhochschule Aachen Abt. Jülich Fax: +49 (241) 6009-5 31 89
Fachbereich 09 email: j.hoffmann@fh-aachen.de

and there two files.

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Sep 21, 2018

I had contacted with the listings package author, here is their reply.

Re: how about add lstget in listings package
Hoffmann, Jobst于2018年9月21日 星期五 下午21:01发送给bubifengyun...查看2个附件
详细信息
关闭按钮
Hi,
Am Freitag, den 21.09.2018, 12:39 +0800 schrieb bubifengyun@sina.com:
> I find that some time, I need to get some state of listings, to produ
> ce some code, like in pandoc. 
> 
> https://github.com/jgm/pandoc/issues/4716
> 
> if there are some code like \lstget{mathescape}, it would be easy to 
> solve such problem. thank you.
> 来自新浪邮箱触屏版
the problem ist, that there is no generic "true" or "false", so you
cannot do something like
if \lstget{mathescape} a \else b \fi
but you have to write
\makeatletter
\lst@ifmathescape mathescape is true\else mathescape is false.\fi
\makeatother
I attach a small example as source and as formatted text.
Kind regards
J. Hoffmann
-- 
Prof. Dr. Jobst Hoffmann Tel: +49 (241) 6009-5 31 59
Fachhochschule Aachen Abt. Jülich Fax: +49 (241) 6009-5 31 89
Fachbereich 09 email: j.hoffmann@fh-aachen.de

and there two files.

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Sep 21, 2018

I had contacted with the listings package author, here is their reply.

Re: how about add lstget in listings package
Hoffmann, Jobst于2018年9月21日 星期五 下午21:01发送给bubifengyun...查看2个附件
详细信息
关闭按钮
Hi,
Am Freitag, den 21.09.2018, 12:39 +0800 schrieb bubifengyun@sina.com:
> I find that some time, I need to get some state of listings, to produ
> ce some code, like in pandoc. 
> 
> https://github.com/jgm/pandoc/issues/4716
> 
> if there are some code like \lstget{mathescape}, it would be easy to 
> solve such problem. thank you.
> 来自新浪邮箱触屏版
the problem ist, that there is no generic "true" or "false", so you
cannot do something like
if \lstget{mathescape} a \else b \fi
but you have to write
\makeatletter
\lst@ifmathescape mathescape is true\else mathescape is false.\fi
\makeatother
I attach a small example as source and as formatted text.
Kind regards
J. Hoffmann
-- 
Prof. Dr. Jobst Hoffmann Tel: +49 (241) 6009-5 31 59
Fachhochschule Aachen Abt. Jülich Fax: +49 (241) 6009-5 31 89
Fachbereich 09 email: j.hoffmann@fh-aachen.de

and there two files.

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Sep 21, 2018

%%% test_ifmathescape.tex ---
%
%% Author: j.hoffmann@fh-aachen.de
%% Version: $Id: test_ifmathescape.tex 0 2018/09/21 12:23:46 ax006ho Exp $

\RequirePackage{ifpdf}
\RequirePackage{ifluatex}

% \documentclass[12pt,a4paper,english,ngerman]{article}

\documentclass[12pt,a4paper,english,ngerman]{scrartcl}
\usepackage{etex}
\makeatletter
\@ifclassloaded{article}{%
  \usepackage{typearea}
}{}
\makeatother

\ifluatex
  \usepackage{fontspec}
  \defaultfontfeatures{Ligatures=TeX}
  \fontspec{Latin Modern Roman}
\else
  \usepackage[utf8]{inputenc}
  \usepackage[T1]{fontenc}
  \usepackage{lmodern}
\fi
\usepackage{babel}

\usepackage{listings}

\begin{document}

\begin{itemize}
  \item Use mathescape local: \lstinline|$x_1$|
    vs. \lstinline[mathescape]|$x_1$|.

  \item Use mathescape global in the current environment:
    \lstset{mathescape=true}\lstinline|$x_1$|
    vs. \lstinline[mathescape]|$x_1$|.

    But: \lstinline[mathescape=false]|$x_1$|
    vs. \lstinline[mathescape]|$x_1$|.
\end{itemize}
Outside the environment:
\makeatletter
\lst@ifmathescape mathescape is true\else mathescape is false.\fi
\makeatother

\lstset{mathescape=true}
And now after setting mathescape global again:
\makeatletter
\lst@ifmathescape mathescape is true\else \fi
\makeatother

As macro one can write in \LaTeX:
\makeatletter
\newcommand{\ifmathescape}[2]{%
  \lst@ifmathescape #1\else #2\fi
}
\makeatother

and use it like \ifmathescape{mathescape is true.}{mathescape is false.}
\lstset{mathescape=false} or
\ifmathescape{mathescape is true.}{mathescape is false.}


\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% TeX-engine: default
%%% TeX-source-correlate-mode: t
%%% mode: flyspell
%%% ispell-local-dictionary: "deutsch8"
%%% End:

@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Sep 21, 2018

I am sorry for that. my phone works slowly, I press too many times comment button.

i think it can add one condition in the output of pandoc.

\ifdisableEscapeString
output the original text, \lstinline!text!
\else
output the escape text,\passthrough\lstinline!text!
\fi

then, user could define themselves \newif\ifEscapeString in other place. for example the \ifmathescape

@jgm
Copy link
Owner

@jgm jgm commented Sep 21, 2018

Good. Perhaps instead of using \passthrough we could use this \ifmathescape macro and generate both possibilities. That might be a way forward, although it would mean including an extra definition in the default template (perhaps conditionally on listings being used) and having more verbose output for listings.

bubifengyun added a commit to bubifengyun/pandoc that referenced this issue Sep 22, 2018
bubifengyun added a commit to bubifengyun/pandoc that referenced this issue Sep 22, 2018
Solve bug jgm#4716,

now, the old text can works, but it need `usepackage{etoolbox}` for `\ifdef`.
if user want to use in mathescape=true for `listings` package, the following can be added to the template when needed.

```latex
\makeatletter
\newcommand{\ifmathescape}[2]{%
  \lst@ifmathescape jgm#1\else jgm#2\fi
}
\makeatother
\let\ifdisableEscapeString\ifmathescape
```
@bubifengyun
Copy link
Author

@bubifengyun bubifengyun commented Jun 27, 2019

I find that this problem still. I want to solve it. Have you any suggestion? Thank you.

basnijholt added a commit to python-adaptive/paper that referenced this issue Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants