Skip to content

Hinting that code-block continues on next page? #3764

Closed

Description

@jessetan

Tables with class longtable can contain page breaks. To hint the reader that the table is not finished, but continues on the next page, the last row on the first page reads "Continued on next page" and the first row on the next page reads "continued from previous page".
Is it possible to do something similar for the code-block directive? A few lines of code on a previous or next page may be overlooked, which makes the code harder to read.

Activity

  1. jfbu commented on May 20, 2017

    @jfbu
    Contributor

    Thanks for feature request, yes it is possible to do something similar.

    Rather than "Continued on next page", an alternative is to use a dashed line for the bottom of the framing, and the top rule of the frame on continuation page. Or even to leave it without any framing.

    Do you have an opinion about this?

    Also @tk0miya should we make effort to provide option to keep former design?

    Currently we (maintainers) have to do the needed coding in sphinx.sty, and this includes interface for user customization. In future usage of tcolorbox as replacement to framed.sty would make styling easier, all customization could go into one tcolorboxoptions Sphinx config, there would be no need for Sphinx to worry about how to implement user customization as it would be passed over to tcolorbox.

  2. added
    type:proposal
    and removed on May 20, 2017
  3. jessetan commented on May 22, 2017

    @jessetan
    ContributorAuthor

    The breakable library of tcolorbox produces nice results as you described.
    Unfortunately we currently use a box without a frame and only background color to indicate code-blocks, so we'd need to find a different way to indicate a block continues o the next page.

  4. jfbu commented on May 22, 2017

    @jfbu
    Contributor

    as proof of concept can you try this:

    latex_elements = {
        'preamble': r'''
    \makeatletter
    \renewcommand\spx@colorbox [3]{%
      \hskip\@totalleftmargin
      \hskip-\fboxsep\hskip-\fboxrule
      \spx@fcolorbox{VerbatimBorderColor}{VerbatimColor}{#1}{#2}{#3}%
      \hskip-\fboxsep\hskip-\fboxrule
      \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth
    }
    \def\spx@fcolorbox #1#2#3#4%
      {\color@b@x {\fboxsep\z@\color{#1}\spx@VerbatimFBox{#3}{#4}}{\color{#2}}}%
    \long\def\spx@VerbatimFBox#1#2#3{%
      \leavevmode
      \begingroup
      \setbox\@tempboxa\hbox{\kern\fboxsep{#3}\kern\fboxsep}%
       \hbox
       {\lower\dimexpr\fboxrule+\fboxsep+\dp\@tempboxa
        \hbox{%
          \vbox{#1%
            % draw frame border _latest_ to avoid pdf viewer issue
                \kern\fboxrule
                \hbox{\kern\fboxrule
                      \vbox{\vskip\fboxsep\copy\@tempboxa\vskip\fboxsep}%
                      \kern-\wd\@tempboxa\kern-\fboxrule
                      \vrule\@width\fboxrule
                      \kern\wd\@tempboxa
                      \vrule\@width\fboxrule}%
                \kern-\dimexpr\fboxsep+\ht\@tempboxa+\dp\@tempboxa
                                      +\fboxsep+\fboxrule\relax
                \hrule\@height\fboxrule
                \kern\dimexpr\fboxsep+\ht\@tempboxa+\dp\@tempboxa+\fboxsep\relax
                \hrule\@height\fboxrule
                #2%
              }%
       }}%
      \endgroup
    }
    \renewenvironment{sphinxVerbatim}{%
      \par
      \parskip\z@skip
      \ifx\sphinxVerbatimTitle\empty
          \addvspace\z@
          \smallskip
          \ifx\sphinxLiteralBlockLabel\empty\else
              \needspace{\sphinxliteralblockwithoutcaptionneedspace}%
              \phantomsection\sphinxLiteralBlockLabel
          \fi
          \setbox\spx@VerbatimTitleBox\box\voidb@x
      \else
         \setbox\spx@VerbatimTitleBox
                \hbox{\begin{minipage}{\linewidth}%
                        \sphinxVerbatimTitle
                      \end{minipage}}%
      \fi
      \fboxsep\sphinxverbatimsep \fboxrule\sphinxverbatimborder
      \ifspx@opt@verbatimwithframe\else\fboxrule\z@\fi
      \ifvoid\spx@VerbatimTitleBox
        \def\FrameCommand{\spx@colorbox{}{}}%
        \def\FirstFrameCommand{\spx@colorbox{}{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa
                          \hb@xt@\z@{\hss(continued on next page)}}}%
      \else
        \def\FrameCommand{\spx@colorbox{%
                % add the caption in a centered way above possibly indented frame
                % hide its width from framed.sty's measuring step
                % note that the caption brings \abovecaptionskip top vertical space
                \moveright\dimexpr\fboxrule+.5\wd\@tempboxa
                      \hb@xt@\z@{\hss\unhcopy\spx@VerbatimTitleBox\hss}}{}}%
        \def\FirstFrameCommand{\spx@colorbox{%
                \moveright\dimexpr\fboxrule+.5\wd\@tempboxa
                      \hb@xt@\z@{\hss\unhcopy\spx@VerbatimTitleBox\hss}}{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa
                          \hb@xt@\z@{\hss(continued on next page)}}}%
      \fi
      \def\MidFrameCommand{\spx@colorbox{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa
                          \hb@xt@\z@{\hss(continued from previous page)}}{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa
                          \hb@xt@\z@{\hss(continued on next page)}}}%
      \def\LastFrameCommand{\spx@colorbox{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa
                          \hb@xt@\z@{\hss(continued from previous page)}}{}}%
      \ifspx@opt@verbatimwrapslines
      \expandafter\def\expandafter\FV@SetupFont\expandafter
        {\FV@SetupFont\sbox\sphinxcontinuationbox {\spx@opt@verbatimcontinued}%
                      \sbox\sphinxvisiblespacebox {\spx@opt@verbatimvisiblespace}}%
      \def\FancyVerbFormatLine ##1{\hsize\linewidth
              \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
                    \doublehyphendemerits\z@\finalhyphendemerits\z@
                    \strut ##1\strut}%
              }%
      \let\FV@Space\spx@verbatim@space
      \sphinxbreaksatspecials
      \def\FancyVerbCodes{\sphinxbreaksviaactive}%
      \fi
      \let\VerbatimEnvironment\sphinxVerbatimEnvironment
      \def\@toodeep {\advance\@listdepth\@ne}%
      \itemsep   \z@skip
      \topsep    \z@skip
      \partopsep \z@skip
      \rightmargin\z@
      \parindent  \z@
      \trivlist\item\relax
         \ifsphinxverbatimwithminipage\spx@inframedtrue\fi
         \ifspx@inframed\noindent\begin{minipage}{\linewidth}\fi
         \MakeFramed {% adapted over from framed.sty's snugshade environment
            \advance\hsize-\width\@totalleftmargin\z@\linewidth\hsize\@setminipage
            }%
         \lineskip\z@skip
         \ifspx@opt@verbatimwrapslines
           \let\verbatim@nolig@list \sphinx@verbatim@nolig@list
         \fi
         \OriginalVerbatim
    }
    {%
      \endOriginalVerbatim
      \par\unskip\@minipagefalse\endMakeFramed % from framed.sty snugshade
      \ifspx@inframed\end{minipage}\fi
      \endtrivlist
    }
    \makeatother
    ''',
    }
    

    a bit scary... I needed to copy over original code to insert the modifications, which is only about 15% of the above. You can find the strings "continued on next page" and "continued from previous page" in the above and customize them.

    In my brief testing, it works for me.

  5. jfbu commented on May 22, 2017

    @jfbu
    Contributor

    notice that the strings (continued on next page), and (continued on previous page) are positioned horizontally just to left of the right frame, or to the left of where the background color ends. Perhaps you want them moved less to the right by an amount of \fboxsep, just add -\fboxsep to the various lines with \moveright. Then the ) will be positioned at end of code lines.

  6. jfbu commented on May 22, 2017

    @jfbu
    Contributor

    if VerbatimBorderColor is set (in 'sphinxsetup'), then this color (which applies to the frame) applies to the strings too... initially unintended but perhaps fine. You can add \normalcolor to the strings to suppress the effect. (i.e. get them in black)

  7. jessetan commented on May 23, 2017

    @jessetan
    ContributorAuthor

    Thanks for your suggestions @jfbu, that works. I have made some small changes to make text adjustment easier. I now have the following in my .sty:

    \renewcommand\spx@colorbox [3]{%
      \hskip\@totalleftmargin
      \hskip-\fboxsep\hskip-\fboxrule
      \spx@fcolorbox{VerbatimBorderColor}{VerbatimColor}{#1}{#2}{#3}%
      \hskip-\fboxsep\hskip-\fboxrule
      \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth
    }
    \def\spx@fcolorbox #1#2#3#4%
      {\color@b@x {\fboxsep\z@\color{#1}\spx@VerbatimFBox{#3}{#4}}{\color{#2}}}%
    \long\def\spx@VerbatimFBox#1#2#3{%
      \leavevmode
      \begingroup
      \setbox\@tempboxa\hbox{\kern\fboxsep{#3}\kern\fboxsep}%
       \hbox
       {\lower\dimexpr\fboxrule+\fboxsep+\dp\@tempboxa
        \hbox{%
          \vbox{#1%
            % draw frame border _latest_ to avoid pdf viewer issue
                \kern\fboxrule
                \hbox{\kern\fboxrule
                      \vbox{\vskip\fboxsep\copy\@tempboxa\vskip\fboxsep}%
                      \kern-\wd\@tempboxa\kern-\fboxrule
                      \vrule\@width\fboxrule
                      \kern\wd\@tempboxa
                      \vrule\@width\fboxrule}%
                \kern-\dimexpr\fboxsep+\ht\@tempboxa+\dp\@tempboxa
                                      +\fboxsep+\fboxrule\relax
                \hrule\@height\fboxrule
                \kern\dimexpr\fboxsep+\ht\@tempboxa+\dp\@tempboxa+\fboxsep\relax
                \hrule\@height\fboxrule
                #2%
              }%
       }}%
      \endgroup
    }
    
    \newcommand{\verbatimContinuesText}{(continued on next page)}
    \newcommand{\verbatimContinuedText}{(continued from previous page)}
    \newcommand{\verbatimContinues}{\normalcolor\footnotesize\verbatimContinuesText}
    \newcommand{\verbatimContinued}{\normalcolor\footnotesize\verbatimContinuedText}
    
    \renewenvironment{sphinxVerbatim}{%
      \par
      \parskip\z@skip
      \ifx\sphinxVerbatimTitle\empty
          \addvspace\z@
          \smallskip
          \ifx\sphinxLiteralBlockLabel\empty\else
              \needspace{\sphinxliteralblockwithoutcaptionneedspace}%
              \phantomsection\sphinxLiteralBlockLabel
          \fi
          \setbox\spx@VerbatimTitleBox\box\voidb@x
      \else
         \setbox\spx@VerbatimTitleBox
                \hbox{\begin{minipage}{\linewidth}%
                        \sphinxVerbatimTitle
                      \end{minipage}}%
      \fi
      \fboxsep\sphinxverbatimsep \fboxrule\sphinxverbatimborder
      \ifspx@opt@verbatimwithframe\else\fboxrule\z@\fi
      \ifvoid\spx@VerbatimTitleBox
        \def\FrameCommand{\spx@colorbox{}{}}%
        \def\FirstFrameCommand{\spx@colorbox{}{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa-\sphinxverbatimsep
                          \hb@xt@\z@{\hss\verbatimContinues}}}%
      \else
        \def\FrameCommand{\spx@colorbox{%
                % add the caption in a centered way above possibly indented frame
                % hide its width from framed.sty's measuring step
                % note that the caption brings \abovecaptionskip top vertical space
                \moveright\dimexpr\fboxrule+.5\wd\@tempboxa
                      \hb@xt@\z@{\hss\unhcopy\spx@VerbatimTitleBox\hss}}{}}%
        \def\FirstFrameCommand{\spx@colorbox{%
                \moveright\dimexpr\fboxrule+.5\wd\@tempboxa
                      \hb@xt@\z@{\hss\unhcopy\spx@VerbatimTitleBox\hss}}{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa-\sphinxverbatimsep
                          \hb@xt@\z@{\hss\verbatimContinues}}}%
      \fi
      \def\MidFrameCommand{\spx@colorbox{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa-\sphinxverbatimsep
                          \hb@xt@\z@{\hss\verbatimContinued}}{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa-\sphinxverbatimsep
                          \hb@xt@\z@{\hss\verbatimContinues}}}%
      \def\LastFrameCommand{\spx@colorbox{%
                \moveright\dimexpr\fboxrule+\wd\@tempboxa-\sphinxverbatimsep
                          \hb@xt@\z@{\hss\verbatimContinued}}{}}%
      \ifspx@opt@verbatimwrapslines
      \expandafter\def\expandafter\FV@SetupFont\expandafter
        {\FV@SetupFont\sbox\sphinxcontinuationbox {\spx@opt@verbatimcontinued}%
                      \sbox\sphinxvisiblespacebox {\spx@opt@verbatimvisiblespace}}%
      \def\FancyVerbFormatLine ##1{\hsize\linewidth
              \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
                    \doublehyphendemerits\z@\finalhyphendemerits\z@
                    \strut ##1\strut}%
              }%
      \let\FV@Space\spx@verbatim@space
      \sphinxbreaksatspecials
      \def\FancyVerbCodes{\sphinxbreaksviaactive}%
      \fi
      \let\VerbatimEnvironment\sphinxVerbatimEnvironment
      \def\@toodeep {\advance\@listdepth\@ne}%
      \itemsep   \z@skip
      \topsep    \z@skip
      \partopsep \z@skip
      \rightmargin\z@
      \parindent  \z@
      \trivlist\item\relax
         \ifsphinxverbatimwithminipage\spx@inframedtrue\fi
         \ifspx@inframed\noindent\begin{minipage}{\linewidth}\fi
         \MakeFramed {% adapted over from framed.sty's snugshade environment
            \advance\hsize-\width\@totalleftmargin\z@\linewidth\hsize\@setminipage
            }%
         \lineskip\z@skip
         \ifspx@opt@verbatimwrapslines
           \let\verbatim@nolig@list \sphinx@verbatim@nolig@list
         \fi
         \OriginalVerbatim
    }
    {%
      \endOriginalVerbatim
      \par\unskip\@minipagefalse\endMakeFramed % from framed.sty snugshade
      \ifspx@inframed\end{minipage}\fi
      \endtrivlist
    }

    Our lead tech writer prefers the tcolorbox-based approach from your initial suggestion though.
    Is a move to tcolorbox an possibility now that the required TexLive version is more recent? (see #3070 and your comments in #3099). Discussion might be better in a separate ticket.

  8. jfbu commented on May 23, 2017

    @jfbu
    Contributor

    The TeXLive is more recent but perhaps not enough so to consider that the tcolorbox version was mature enough, see http://www.ctan.org/ctan-ann/pkg/tcolorbox and you will see that much activity on tcolorbox side happened since February 2014. I expect the changes to be mostly backwards compatible though and also the default design of Sphinx appears minimal enough (frame and background color) to hope that syntax what worked in 2014 still works identically in 2017.

    Definitely, tcolorbox is on our road-map (as discussed already at #3099). It could be added to 1.7. I am thinking of shipping the needed code in a separate style file which would get activated only via a config option or a 'sphinxsetup' option (and then replace the default definitions done in sphinx.sty). One worry indeed is about all the overhead of loading TikZ, and the fact that TikZ had sometimes backwards incompatible changes, and at Sphinx we can not suddenly tell people they must have the must up-to-date TeX install after having supported for years 2009 set-up ... between framed.sty and tcolorbox+pgf the code ratio is probably of 1 to 50 at least (framed.sty has about 300 lines of LaTeX code). And surely tcolorbox requires xcolor for example, and quite a few other dependencies.

    About your abstraction of the continuation strings, the definitions must be extracted from latex style file and put into document preamble by latex writer, in order for internationalization to be possible. I will make this when I get time. Thanks for input!

  9. jfbu commented on May 23, 2017

    @jfbu
    Contributor

    ouch... thanks to this thread I realize the \fboxsep parameter when used in \spx@VerbatimFBox is zero (see the \fboxsep\z@ in \def\spx@fcolorbox.) In fact that piece of code is entirely devoted to draw the frame. The verbatimsep parameter of 'sphinxsetup' has already been taken account of for the contents (separation of code from the frame, or if no frame, from the border of the colored region, both horizontally and vertically).

    Indeed I was intrigued that my advice about adding -\fboxsep, which you followed, was without effect. What you should do is use -\sphinxverbatimsep, not -\fboxsep.

    All the various \kern\fboxsep or \vskip\fboxsep inside \spx@VerbatimFBox whether the one for this thread or the one currently in Sphinx 1.6.1 and earlier are strictly without any effect, because \fboxsep is zero. With a non zero \fboxsep the frame would be drawn at some distance of the colored region. I had a misunterstanding in the timing of \spx@fcolorbox expansion, but the output was the expected one with the \fboxsep\z@ I had inserted in \def\spx@fcolorbox and I never came back to the earlier code of \spx@VerbatimFBox.

    Added comment at 2d15032

  10. jessetan commented on May 23, 2017

    @jessetan
    ContributorAuthor

    Thanks, I saw no effect with \fboxsep, but I thought it was one of my other changes and had no time to debug. Using \sphinxverbatimsep works, I've updated the code above.

  11. jfbu commented on May 23, 2017

    @jfbu
    Contributor

    sorry for inconvenience ... fortunately you did not waste time debugging this.

  12. jessetan commented on May 23, 2017

    @jessetan
    ContributorAuthor

    I've filed #3790 for discussions regarding tcolorbox. No need to complicate this issue if the "continued on next page" solves the problem.

  13. added a commit that references this issue on May 23, 2017
    e1d7e3f
  14. jfbu commented on May 23, 2017

    @jfbu
    Contributor

    +1 for tcolorbox devoted issue. At #3792 I have implemented an extension of your abstraction of the continued and continues texts. It will be easier to customize even more the framing, if needed, as shorter code snippets are extracted into \sphinxVerbatimFirstFrameCommand etc...

  15. jfbu commented on May 23, 2017

    @jfbu
    Contributor

    forgot to say that in the #3792, there is again use of \fboxsep as it is not set to zero anymore at the same location, the point being that its use has been removed from core framing macro \spx@VerbatimFBox.

  16. added this to the 1.7 milestone on May 28, 2017
  17. added 3 commits that reference this issue on Jun 13, 2017
    5673e46
    c2277ae
    c7914b3
  18. jfbu commented on Jun 17, 2017

    @jfbu
    Contributor

    #3792 was merged in stable for 1.6.3 release (c7914b3). By default, the feature is not activated. It will be at 1.7. Extension for 1.7 is proposed at #3872.

  19. locked as resolved and limited conversation to collaborators on Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

builder:latextype:proposal

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions