使用 Verbatim 拆分 vbox

使用 Verbatim 拆分 vbox

我已将Verbatim环境替换为包含vbox和,colorbox以便可以为其应用背景颜色。问题是现在它在页面末尾没有分割。

这是一个最小的工作代码(工作意味着它可以编译):

\documentclass[
a4paper,
]{article}

\usepackage{xcolor}

\usepackage{fvextra}

\definecolor{codebgcolor}{cmyk}{0,0.02,0.03,0}

% Note: redefining the Verbatim environment to have a background
% I used `latexdef -E itemize` and `latexdef -E enumerate` to
% discover the internal variables they use to control indentation.
% Then I used `\leftmargin` to know how much each indentation
% level indents, and added a negative horizontal spacing `\hspace`.
% ref: [The definitions of LaTeX commands](https://texfaq.org/FAQ-ltxcmds)
% ref: https://tex.stackexchange.com/questions/62202/change-background-colour-of-verbatim-environment
\makeatletter
\let\oldv\Verbatim
\def\Verbatim{%
    \setbox0\vbox\bgroup\oldv%
}
% == indented nested Verbatim ==
% ref: https://tex.stackexchange.com/questions/426736/addition-multiplication-of-parameter-and-constant-with-unit
\def\unindent{\hspace{-\dimexpr\leftmargin*(\@itemdepth+\@enumdepth)\relax}}
\let\oldendv\endVerbatim
\def\endVerbatim{%
    \oldendv\egroup\fboxsep0pt \colorbox{codebgcolor}{%
        \parbox{\linewidth}{%
            \unindent\usebox0%
        }%
    }%
}
\makeatother

\begin{document}
    
\begin{itemize}
\item Something here:

    \begin{Verbatim}[commandchars=\\\{\}, mathescape]
    
    a a
    a b
    a c
    a d
    a e
    a f
    a g
    a h
    a i
    a j
    a k
    a l
    a m
    a n
    a o
    a p
    a q
    a r
    a s
    a t
    a u
    a v
    a w
    a x
    a y
    b a
    b b
    b c
    b d
    b e
    b f
    b g
    b h
    b i
    b j
    b k
    b l
    b m
    b n
    b o
    b p
    b q
    b r
    b s
    b t
    b u
    b v
    b w
    b x
    b y
    c a
    c b
    c c
    c d
    c e
    c f
    c g
    c h
    c i
    c j
    c k
    c l
    c m
    c n
    c o
    c p
    c q
    c r
    c s
    c t
    c u
    c v
    c w
    c x
    c y
    
    \end{Verbatim}

\end{itemize}

\end{document}

我尝试按照另一个问题中的说明进行操作可破坏的垂直盒子没有成功。

我真的不明白\partialboxdim接受的答案是什么,我该如何设置它。另外,我不知道如何将它与 一起使用\setbox0\vbox\bgroup。我尝试将其替换为\setbox\totalbox=\vbox\bgroup,然后在重新定义时使用它endVerbatim替换\usebox0\totalbox... ,但这没有用。我不知道该怎么做。

答案1

您应该使用 mdframed、framed 或 tcolorbox 包之一来制作可破坏的颜色框。例如

\documentclass[
a4paper,
]{article}

\usepackage{xcolor}
\usepackage{framed}
\usepackage{fvextra}

\definecolor{codebgcolor}{cmyk}{0,0.02,0.03,0}

\let\oldv\Verbatim
\def\Verbatim{%
    \definecolor{shadecolor}{cmyk}{0,0.02,0.03,0}%
    \csname shaded*\endcsname \oldv
}
\let\oldendv\endVerbatim
\def\endVerbatim{%
  \oldendv \csname endshaded*\endcsname
}

\begin{document}

This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.

\begin{itemize}
\item Something here:

  \begin{Verbatim}[commandchars=\\\{\}, mathescape]
    
    a a
    a b
    a c
    a d
    a e
    a f
    a g
    a h
    a i
    a j
    a k
    a l
    a m
    a n
    a o
    a p
    a q
    a r
    a s
    a t
    a u
    a v
    a w
    a x
    a y
    b a
    b b
    b c
    b d
    b e
    b f
    b g
    b h
    b i
    b j
    b k
    b l
    b m
    b n
    b o
    b p
    b q
    b r
    b s
    b t
    b u
    b v
    b w
    b x
    b y
    c a
    c b
    c c
    c d
    c e
    c f
    c g
    c h
    c i
    c j
    c k
    c l
    c m
    c n
    c o
    c p
    c q
    c r
    c s
    c t
    c u
    c v
    c w
    c x
    c y
    
  \end{Verbatim}

\end{itemize}

This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
\end{document}

我不太确定您想要如何放置阴影,但我认为您试图将其放置在常规文本边距处。在 framed.sty 中,还有其他变体,例如shadedsnugshadesnugshade*。使用snugshade*可使阴影与列表一起缩进。

其它包也做得很好!(不过祝你阅读 tcolorbox 手册顺利)。

答案2

你应该考虑一下tcolorbox

\documentclass[
a4paper,
]{article}

\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins}

\definecolor{codebgcolor}{cmyk}{0,0.05,0.05,0}

\newtcblisting{Verbatim}{
  breakable,enhanced,
  listing only,
  frame hidden,
  colback=codebgcolor,
  grow to left by=6pt,
  grow to right by=6pt,
  left=1.6pt,
  right=1.6pt,
  arc=0pt
}

\begin{document}

\begin{Verbatim}
abc def
ghi
\end{Verbatim}
    
\begin{itemize}
\item Something here:

\begin{Verbatim}
    a a
    a b
    a c
    a d
    a e
    a f
    a g
    a h
    a i
    a j
    a k
    a l
    a m
    a n
    a o
    a p
    a q
    a r
    a s
    a t
    a u
    a v
    a w
    a x
    a y
    b a
    b b
    b c
    b d
    b e
    b f
    b g
    b h
    b i
    b j
    b k
    b l
    b m
    b n
    b o
    b p
    b q
    b r
    b s
    b t
    b u
    b v
    b w
    b x
    b y
    c a
    c b
    c c
    c d
    c e
    c f
    c g
    c h
    c i
    c j
    c k
    c l
    c m
    c n
    c o
    c p
    c q
    c r
    c s
    c t
    c u
    c v
    c w
    c x
    c y
\end{Verbatim}

\end{itemize}

\end{document}

在此处输入图片描述

我稍微改变了你的颜色,以使背景更加清晰可见。

相关内容