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

[feature request] autolinebreak for fenced code under skylighting #4302

Closed
jannick0 opened this issue Jan 25, 2018 · 15 comments
Closed

[feature request] autolinebreak for fenced code under skylighting #4302

jannick0 opened this issue Jan 25, 2018 · 15 comments

Comments

@jannick0
Copy link

Pandoc 2.0.6 / skylighting 0.5.0.1:

It appears that for fenced code blocks with long lines there is no autolinebreak feature (for style pygments, at least).

Example:
pandoc -f markdown -t latex -o long-code-line.pdf long-code-line.md

long-code-line.md:
~~~
   this is a very long long long long long long long long long long long long long line which is not broken
~~~

NB: Options of the LaTeX listings package:

  • breaklines (boolean)
  • breakatwhitespace (boolean)
  • breakautoindent
  • linewidth

Apologies if there is a trivial solution to this issue making this feature request obsolete.

@jgm
Copy link
Owner

jgm commented Jan 25, 2018

I don't think this can be implemented in skylighting itself, because skylighting has no way of knowing how much space each token takes and hence when the end of the line is reached.

If there's some latex magic that can cause auto wrapping inside a custom verbatim environment defined using fancyvrb, then we could use that.

@jannick0
Copy link
Author

jannick0 commented Jan 26, 2018

On the LaTeX side I believe the package fvextra (github) could be a nice candidate doing all the tedious work by directly talking to - or rather through - fancyvrb.

I had a short look at fvextra and it looks like it covers many - if not all - options of listings. One of the main and nasty issues is that a broken line should not have a new code line number. Probably worth while having a look at the other package features, too, to see if they could be used in pandoc.

For the solution only two tiny edits to the tex code are needed:

%% add
\usepackage{fvextra}
...
%% add the breaklines option like so
\begin{Highlighting}[breaklines,numbers=left,,]
...

Please feel free to close this issue as you may find appropriate.


@jgm Separately, John, two additional observations:

  1. Lexer - Leading tabs in the code section are glued to the first token which I am not sure is the intension. Of course, it does not affect the output, but I would have expected the AST to do the white space book keeping separately. Tabs within the line share their leading friends' destiny. Again, this is a bit far off, I just wanted to share this with you.

Example:

~~~
[tab]token
~~~

gets translated to

\NormalTok{[tab]token}
  1. Documentation - I was a bit confused that --wrap did not solve the code line breaking issue. So I would suggest to add a comment to the documentation saying that this option applies to standard text only.

HTH.

@jannick0
Copy link
Author

These edits in the preamble would work, too:

\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

@georgeraraujo
Copy link

These edits in the preamble would work, too:

\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

How about prepending to verbatim instead of creating a custom environment?

% single line spacing for code
\usepackage{etoolbox}
\BeforeBeginEnvironment{verbatim}{\def\baselinestretch{1}}

@jannick0
Copy link
Author

baselinestretch changes the spacing in everything of the document after its has been triggered. That is not the aim of the proposed feature, namely line beaks of long lines in fenced code blocks. So I can not see how this would help solving the issue.

BTW - my suggestion would amend an already existing 'custom' environment created by the current version of pandoc. The adjustment is tiny, and it fully addresses the issue.

Or am I missing anything here, since I am lightyears away from being a LaTeX expert?

@georgeraraujo
Copy link

baselinestretch changes the spacing in everything of the document after its has been triggered. That is not the aim of the proposed feature, namely line beaks of long lines in fenced code blocks. So I can not see how this would help solving the issue.

Sorry, now I see expressed myself poorly. That was just an example of how it is possible to prepend commands to the existing verbatim environment instead of creating a new one...

BTW - my suggestion would amend an already existing 'custom' environment created by the current version of pandoc. The adjustment is tiny, and it fully addresses the issue.

... but pandoc already does it, so your suggestion to amend looks good to me.

@aubertc
Copy link

aubertc commented Jan 26, 2018

I also think @jannick0 's suggestion perfectly fits the bill. I took the liberty to mention it as an answer to a question asked on StackExchange, cf. https://tex.stackexchange.com/a/412316/34551

@noraj
Copy link

noraj commented Sep 28, 2019

Writing a technical report with code blocks or long string is really a nightmare. Even with fvextra and the breakanywhere option I did not succeed to do something about very long string.
When it's code it is easy to split strings but hen you just want to put the output of a command with very long lines you can't do that.

@jgm
Copy link
Owner

jgm commented Sep 28, 2019

When it's code it is easy to split strings but hen you just want to put the output of a command with very long lines you can't do that.

If you don't care about where they split, you can use a lua filter to add line breaks automatically.

@noraj
Copy link

noraj commented Sep 28, 2019

I'm using markdown to PDF conversion through a LaTeX template made by someone else. I'll rather avoid to have to tweak it.

@jgm
Copy link
Owner

jgm commented Sep 28, 2019

A filter doesn't require changes to tempate.

@noraj
Copy link

noraj commented Sep 28, 2019

@jgm I imagined it was something you put in your LaTeX document, but I looked for it an I saw it was an additional option to pass to pandoc. This make an extra file and I know anything about lua lang.

@tarleb
Copy link
Collaborator

tarleb commented May 21, 2021

Pandoc tries to use only packages which are shipped with texlive-latex-recommended in Debian-based distros. The suggested package fvextra is in texlive-latex-extra. I believe that solving linebreaks in code is out of scope for pandoc.

My personal suggestion would be to use minted, e.g. via the minted Lua filter.

@doitian
Copy link

doitian commented Apr 14, 2023

My workaround to handle the code blocks without a language is setting the language to text via a lua filter

function CodeBlock(el)
  if #el.classes == 0 then
    el.classes[1] = 'text'
  end
  return el
end

return {{CodeBlock = CodeBlock}}

Save the file and use it via the pandoc command line option: --lua-filter path/to/the/filter.lua.

@tigerhawkvok
Copy link

Perhaps I took a hammer to an egg here, but I was having a hard time line breaking code of regex so I needed break-anywhere. I ended up with this:

---
format:
    html:
        code-fold: true
    pdf:
        header-includes:
            - "\\usepackage{fvextra}"
            - "\\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\\\\{\\}, breaklines, breaknonspaceingroup, breakanywhere}"
            - "\\fvset{breaklines}"
            - "\\fvset{breaknonspaceingroup}"
            - "\\fvset{breakanywhere}"
latex-auto-install: true
---

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

9 participants