左侧带线的嵌套易碎盒(任何包装)

左侧带线的嵌套易碎盒(任何包装)

我正在尝试创建一个类似的环境quotation,但左侧有一条线。我还希望它支持多个段落、脚注、数学环境和浮点数(基本上,您应该能够将其环绕在任何或大多数内容周围,它应该可以正常工作)。

困难的是我希望它能够处理分页符,即使嵌套也是如此。来自 TeXFAQ 条目打破文本框,我知道可以用博伊特斯包,但不幸的是,它会破坏浮动和脚注,所以这对我来说不是一个选择。相似的 问题之前,但针对的是整个框,而不是仅针对左线。我不确定这个问题是否更容易回答。我还知道,显然无法使用framedmdframedtcolorbox包来解决这个问题(请参阅包文档)。

有一个类似问题关于mdframed具体问题,而不是关于一般问题,但答案中的框不能用于任意内容。(上一个问题被标记为重复,这是为了澄清为什么我认为它不是)

这能做到吗?

例子: 例子

示例代码改编自类似问题

\documentclass{article}

\usepackage{framed}
\usepackage[parfill]{parskip}

\newenvironment{quotationb}%
{\begin{leftbar}}%
{\end{leftbar}}

\begin{document}

\begin{quotationb}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\begin{quotationb}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\begin{quotationb}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{quotationb}
Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{quotationb}
Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{quotationb}
\end{document}

答案1

以下是一个足够的选项。它使用eso-pic绘制垂直规则,允许嵌套和调整缩进和规则宽度。

在此处输入图片描述

\documentclass{article}

\usepackage{lipsum}
\usepackage{changepage,eso-pic}
\usepackage[savepos,abspage,user]{zref}

\newcounter{newleftbar}
\newlength{\leftbarindent}
\newlength{\leftbarwidth}
\setlength{\leftbarwidth}{2pt}

\makeatletter
\NewDocumentEnvironment{leftbar}{ O{2em} }{%
  \setlength{\parindent}{0pt}%
  \def\@leftbarindent{#1}%
  \addtolength{\leftbarindent}{\@leftbarindent}%
  \edef\leftbarnest{\thenewleftbar}%
  \stepcounter{newleftbar}% New leftbar environment
  \edef\theleftbar{\thenewleftbar}%
  \begin{adjustwidth}{#1}{0pt}
    \leavevmode% Start paragraph
    \zsavepos{leftbar-\leftbarnest-\theleftbar-top}% Save position of left bar top
    \zlabel{leftbar-\leftbarnest-\theleftbar-top-page}% Save page of left bar top
    % Store left bar top's text block top/bottom
    \begingroup\edef\x{\endgroup\noexpand\AddToShipoutPictureFG*{%
      \noexpand\AtTextUpperLeft{\noexpand\zsavepos{leftbar-\leftbarnest-\theleftbar-ftt}}%
      \noexpand\AtTextLowerLeft{\noexpand\zsavepos{leftbar-\leftbarnest-\theleftbar-ftb}}%
    }%
    \noexpand\AddToShipoutPictureFG{%
      \noexpand\ifnum\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-top-page}{abspage}{0}=\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-bottom-page}{abspage}{0}
        % Single page left bar
        \noexpand\ifnum\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-top-page}{abspage}{0}=\noexpand\arabic{abspage}
          \noexpand\put(\dimexpr\zposx{leftbar-\leftbarnest-\theleftbar-top}sp-#1,\dimexpr\zposy{leftbar-\leftbarnest-\theleftbar-bottom}sp-.2\baselineskip){%
            \noexpand\rule{\leftbarwidth}{\dimexpr\zposy{leftbar-\leftbarnest-\theleftbar-top}sp-\zposy{leftbar-\leftbarnest-\theleftbar-bottom}sp+\baselineskip}}%
        \noexpand\fi
      \noexpand\fi
      \noexpand\ifnum\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-top-page}{abspage}{0}<\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-bottom-page}{abspage}{0}
        % Multipage left bar
        \noexpand\ifnum\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-top-page}{abspage}{0}=\noexpand\arabic{abspage}
          % First page bar
          \noexpand\put(\dimexpr\zposx{leftbar-\leftbarnest-\theleftbar-top}sp-#1,\zposy{leftbar-\leftbarnest-\theleftbar-ftb}sp){%
            \noexpand\rule{\leftbarwidth}{\dimexpr\zposy{leftbar-\leftbarnest-\theleftbar-top}sp-\zposy{leftbar-\leftbarnest-\theleftbar-ftb}sp+.8\baselineskip}}%
        \noexpand\fi
        \noexpand\ifnum\noexpand\arabic{abspage}>\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-top-page}{abspage}{0}
          \noexpand\ifnum\noexpand\arabic{abspage}<\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-bottom-page}{abspage}{0}
            % Middle page bar
            \noexpand\put(\dimexpr\zposx{leftbar-\leftbarnest-\theleftbar-top}sp-#1,\dimexpr\zposy{leftbar-\leftbarnest-\theleftbar-ftb}sp-.2\baselineskip){%
              \noexpand\rule{\leftbarwidth}{\dimexpr\zposy{leftbar-\leftbarnest-\theleftbar-ftt}sp-\zposy{leftbar-\leftbarnest-\theleftbar-ftb}sp}}%
          \noexpand\fi
        \noexpand\fi
        \noexpand\ifnum\noexpand\zref@extractdefault{leftbar-\leftbarnest-\theleftbar-bottom-page}{abspage}{0}=\noexpand\arabic{abspage}
          % Last page bar
          \noexpand\put(\dimexpr\zposx{leftbar-\leftbarnest-\theleftbar-top}sp-#1,\dimexpr\zposy{leftbar-\leftbarnest-\theleftbar-bottom}sp-.2\baselineskip){%
            \noexpand\rule{\leftbarwidth}{\dimexpr\zposy{leftbar-\leftbarnest-\theleftbar-ltt}sp-\zposy{leftbar-\leftbarnest-\theleftbar-bottom}sp+.2\baselineskip}}%
        \noexpand\fi
      \noexpand\fi
    }}\x
    \ignorespaces
}{%
    \zlabel{leftbar-\leftbarnest-\theleftbar-bottom-page}% Save page of let bar bottom
    \zsavepos{leftbar-\leftbarnest-\theleftbar-bottom}% Save position of left bar bottom
    % Store left bar bottom's text block top/bottom
    \begingroup\edef\x{\endgroup\noexpand\AddToShipoutPictureFG*{%
      \noexpand\AtTextUpperLeft{\noexpand\zsavepos{leftbar-\leftbarnest-\theleftbar-ltt}}%
      \noexpand\AtTextLowerLeft{\noexpand\zsavepos{leftbar-\leftbarnest-\theleftbar-ltb}}%
    }}\x%
  \end{adjustwidth}
  \addtolength{\leftbarindent}{-\@leftbarindent}%
}
\makeatother

\begin{document}

\lipsum[1]

\begin{leftbar}
  \lipsum[2]
  
  \begin{leftbar}[1em]
    \lipsum[3-12]
  \end{leftbar}
  
  \lipsum[13]
\end{leftbar}

\lipsum[14]

\begin{leftbar}[5pt]
  \lipsum[15]
\end{leftbar}

\lipsum[16]

\begin{leftbar}
  \vspace{-\baselineskip}
  \begin{leftbar}
    \lipsum[17]
  \end{leftbar}
\end{leftbar}

\lipsum[18]

\begin{leftbar}
  \lipsum[19]
  \begin{leftbar}
    \lipsum[20]
  \end{leftbar}
\end{leftbar}

\lipsum[21]

\begin{leftbar}
  \lipsum[22]
  \begin{leftbar}
    \lipsum[23]
    \begin{leftbar}
      \lipsum[24]
      \begin{leftbar}
        \lipsum[25]
      \end{leftbar}
      \lipsum[26]
    \end{leftbar}
    \lipsum[27]
  \end{leftbar}
  \lipsum[28]
\end{leftbar}

\lipsum[29]

\end{document}

zref用于捕获leftbar文本块的开始和结束位置以及绝对页码,以识别是leftbar位于单个页面上还是必须跨越多个页面。在后一种情况下,该栏被拆分为“首页栏”、“中间页栏”(可能不存在)和“最后一页栏”。

oneside对于文档或具有类似边距的文档,这应该足够了twoside。如果文本块偏移(如在twoside模式中),则可能存在放置问题,或者必须更新代码以适应这种情况。

对于某一层无内容的嵌套检查做得并不多,因此需要手动进行垂直调整。此外,\end{leftbar} \end{leftbar}由于与 之间的间距,多个连续闭包 ( ) 可能会出现一些错位adjustwidth

相关内容