Repository navigation
Allow linebreaks in latex(pdf) codeblocks - #2343
Conversation
|
@jfbu |
|
When I use your implementation I get a problem in |
|
@jnaulty no special changes. The PR does not use |
|
I setup a virtualenv with your repo. |
|
@jnaulty I have had random errors of that type in unrelated topic branches, I did make latexpdf again and the error went away ... |
|
hmm, it has this error on each new build. Rerunning it solves the problem, but I don't know why. Any ideas? |
|
Strange, I have not encountered persistent errors of that type. I don't see how it could be related to my patch which only modifies |
|
how do I patch stable with your changes? |
|
If you have cloned my repo, |
|
Sorry I meant |
|
😄 👍 |
1ce04d0 to
8473ae9
Compare
|
I have replaced the series of commits in this PR by a unique final one. This new presentation is more compact for easier reading of LaTeX code for review, and revised macro names. I try for consistency to always use The commit is still based on top of 1.3.5. release. |
|
I found an issue, with the default settings, the following rst file fails to find the correct spot to break line: ::
xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x
- Good morning
::
xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x |
|
good find thanks ::
xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x
- Good morning
* Hello
::
xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x xx x x x shows that clearly the problem is with informing the code about the left indent. Ok I have a fix which I am pushing now, but it is night here, I will investigate better tomorrow. |
35463c1 to
9124f69
Compare
|
I have rebased on current stable, fixing conflicts in passing, and squashed into a single commit. |
| % These are user customizable e.g. from latex_elements's preamble key. | ||
| % Use of \textvisiblespace for compatibility with XeTeX/LuaTeX/fontspec. | ||
| \newcommand*\Sphinxvisiblespace | ||
| {\textcolor{red}{\textvisiblespace}} |
There was a problem hiding this comment.
Oh, sorry. I understand what Sphinxvisiblespace means. It's OK to use \textcolor{red}.
There was a problem hiding this comment.
should we make this color customizable ? it is possible already for the user to use 'preamble' key to do own definition to overwrite the one from sphinx.sty.
|
It works fine for me. |
Also related to sphinx-doc#1534. Convert code lines into flushleft paragraphs, thus allowing linebreaks at spaces, and use pre-existing Pygments mark-up to also allow line breaks at (type 1) characters {, <, #, %, $, ' and " and (type 2) _, }, ^, &, >, -, ~, ., ;, :, ?, !, / and the comma. Characters of type 1 go to next line in case of break, those of type 2 stay on same line. - no new LaTeX package is used, but current use of fancyverb modified. - code line numbering is not affected (extra lines are not numbered. - spacing of top line and last line with respect of frame is improved. - near bottom of page the wrapping of lines longer than text width, which formerly continued into right margin, can produce one or more extra lines at bottom of page. It looks impossible without extensive rewrite of fancyvrb.sty or the switch to an altogether different LaTeX package doing source code formatting to allow page breaks there.
9124f69 to
ac7d7b5
Compare
|
@tk0miya on existing Sphinx projects not having literal blocks with lines longer than text width, the impact will be that top frame is a bit higher compared to first baseline and bottom frame a bit lower compared to last baseline, because now a I have rebased on current tip of stable branch and will update CHANGES. |
|
I am not sure if my additions to |
Indeed, LaTeX's handling of package options is not robust when options contain LaTeX macros; thus we pass all the user customization to ``\sphinxsetup``, not as package options to "sphinx.sty". Also, make parameters for long code line breaking (ref sphinx-doc#2343, 1.4.2) accessible as sub-keys of 'sphinxpackageoptions'. The space reserved for the continuation character has been modified for coherence and compatibility with use in footnotes. In fact it did not use the correct font for the dimension computation. The documentation explains how to recover former value.
|
What would be involved in getting a similar feature enabled for the |
|
@spencer-rig We should investigate inside of the directive to do that or not to do. |
This addresses the issue raised in #1534 and #2304: currently long lines do not wrap in code blocks for LaTeX output, they cross the frame, go to the page margin and possibly beyond.
Currently, Sphinx uses LaTeX package
fancyvrbfor itsVerbatimenvironment. This environment scans each input line and puts it in an unbreakable horizontal box (actually multiple nested ones). It does not do any syntax highlighting, the latter has been incorporated by Sphinx LaTeX writer directly via macros\PYG...in the produced latex file.More recent packages exist, among them
mintedwhich however needs-shell-escapefor its latex runs as it appeals topygmentize. Switching tomintedwould mean presumably a complete revision of how the Sphinx LaTeX builder operates. There is a sub-part ofmintedwhich modifiesfancyvrb'sVerbatimto allow line-breaks, and this does not require-shell-escapebut only "draft" option, but this is not available separately so far and consists of at least 300 hundred lines of code possibly using more stuff from other LaTeX packages (for key-value handling for example).I propose this patch which is a minimal modification of
fancyvrb'sVerbatim: the macro\FancyVerbFormatLineis modified to wrap its arguments first in a paragraph building box. Then, linebreaks can occur at spaces (the\FV@Spaceis modified to allow such a line break.)But having break-points only at spaces is not enough (in my testing of
mintedit did not do more than that in its modification offancyvrb'sVerbatim) Thus it is proposed to exploit the already existing Pygmentization and modify (only in the Verbatim naturally) thePYGZus, etc... macros, which end up in the document LaTeX preamble; this modification is done to insert potential break points. For some characters like the?or the/which do not seem to be treated by pygmentization another approach is used which consists of making them "catcode active" (only in the Verbatim naturally).This depends on new macros
\Sphinxbreaksatspecialsand\Sphinxbreaksatpunctwhich can be redefined to do nothing if the behaviour is not desired. Either from the user via something in the'preamble'key inconf.py, or possibly via a dedicated new option.When a line-break occurs, a continuation symbol will be displayed at start of second line. This symbol and the amount of indentation from the left margin are set in macros the user can redefine in the
'preamble'. For the space character a red visible space is printed at end of broken line. This can also be customized via certain\Sphinx...macros.Some characters where breaks are allowed will stay on first line, others go to continuation line. See code comments.
The new
Verbatimmakes optionobeytabsa no-op but I don't think this option was available via Sphinx (it specified to use tabs from the input as tabulation stops in the latex (pdf) output, like for example in an Emacs buffer; from looking the Sphinx docs, I got the impression no option was provided to handle tab-formatted source code, perhaps I am wrong).The extra lines do not modify the numbering: the first of the lines is still the numbered one.
It may happen that at bottom of page, the extra lines spill over the verbatim frame (by the way, although fancyvrb has the ability to draw frames, for some reason sphinx.sty uses
framedenvironment for that; this is not impacted). It is impossible to allow page breaks at these locations if not rewriting core parts offancyvrblatex code. At least the material is now visible by the reader it does not disappear to the right of the printed page. Notice that the exact same issue arises when usingmintedwith "draft" option and\fvset{breaklines=true}The core changes in this patch are only about ten lines in
sphinx.styto allow breaks at spaces. Further changes handle the breaks at characters. Note that digits or letters will not allow linebreaks, but this could be added.As an after effect the spacing with the top rule and bottom rule of the surrounding frame are improved.