Tcolorbox 中的 Multicols 很快就会破坏页面 Multicols(并且不会监听 pageheight)

Tcolorbox 中的 Multicols 很快就会破坏页面 Multicols(并且不会监听 pageheight)

从编译以下 MWE 时可以看出,页面高度(2 英寸)允许每页有 11 行文本(参见第 1 页),因为第十二行第十三行发送至第 2 页。

在这十三行之后,我引入了另外十三行文本,但它们位于 Multicols 环境中。

  1. 标题解释:Tcolorbox 中的 Multicols 很快就会破坏页面

令人讨厌的是,多列行没有在前几行之后立即添加(即第一行(多列内)之后没有启动第十三行,而是由于 Multicol “太长” 而无法放在同一页面上,因此被发送到下一页)。

  1. 标题解释:Tcolorbox 中的 Multicols 不监听 pageheight

然后,人们可以注意到 Tcolorbox 中的行实际上在 11 行之后并没有中断,而是远远超出了这个范围(参见第 3 页溢出的行)。

注意:我认为这些行为与 Tcolorbox 没有特别关系(而是 Multicols 所特有的)。无论如何,MWE 展示了我如何解决这个问题,同时 Tcolorbox 可以直观地澄清一些事情(例如页面高度)。

\documentclass{article}

\usepackage{multicol}
\usepackage[a4paper, total={7in, 2in}]{geometry}
\usepackage[breakable]{tcolorbox}
\newcommand\tbox[1]{\begin{tcolorbox}[breakable]#1\end{tcolorbox}}
\begin{document}

\tbox{

First line\\
Second line\\
Third line\\
Fourth line\\ 
Fifth line\\
Sixth line\\
Seventh line\\
Eighth line\\
Tenth line\\
Eleventh line\\
Twelfth line\\
Thirteenth line\\

\begin{multicols}{2}

First line (inside multicol)\\
Second line (inside multicol)\\
Third line (inside multicol)\\
Fourth line (inside multicol)\\
Fifth line (inside multicol)\\
Sixth line (inside multicol)\\
Seventh line (inside multicol)\\
Eighth line (inside multicol)\\
Tenth line (inside multicol)\\
Eleventh line (inside multicol)\\
Twelfth line (inside multicol)\\
Thirteenth line (inside multicol)\\ \columnbreak

Next to first line (inside multicol)\\
Next to second line (inside multicol)\\
Next to third line (inside multicol)\\
Next to fourth line (inside multicol)\\
Next to fifth line (inside multicol)\\
Next to sixth line (inside multicol)\\
Next to seventh line (inside multicol)\\
Next to eighth line (inside multicol)\\
Next to tenth line (inside multicol)\\
Next to eleventh line (inside multicol)\\
Next to  twelfth line (inside multicol)\\
Next to thirteenth line (inside multicol)\\ 

\end{multicols}

}

\end{document}

答案1

既然你想手动控制线路,我就这么做

\documentclass{article}

\usepackage{multicol}
\usepackage[a4paper, total={7in, 2in}]{geometry}
\usepackage[breakable]{tcolorbox}
\newcommand\tbox[1]{\begin{tcolorbox}[breakable]#1\end{tcolorbox}}
\newcommand\x[2]{%
\noindent\makebox[.5\textwidth][l]{#1}%
\makebox[.5\textwidth][l]{#2}%
}
\begin{document}

\tbox{

\x{%
First line (inside multicol)}{%
Next to first line (inside multicol)}

\x{%
Second line (inside multicol)}{%
Next to second line (inside multicol)}

\x{%
Third line (inside multicol)}{%
Next to third line (inside multicol)}

\x{%
Fourth line (inside multicol)}{%
Next to fourth line (inside multicol)}

\x{%
Fifth line (inside multicol)}{%
Next to fifth line (inside multicol)}

\x{%
Sixth line (inside multicol)}{%
Next to sixth line (inside multicol)}

\x{%
Seventh line (inside multicol)}{%
Next to seventh line (inside multicol)}

\x{%
Eighth line (inside multicol)}{%
Next to eighth line (inside multicol)}

\x{%
Tenth line (inside multicol)}{%
Next to tenth line (inside multicol)}

\x{%
Eleventh line (inside multicol)}{%
Next to eleventh line (inside multicol)}

\x{%
Twelfth line (inside multicol)}{%
Next to  twelfth line (inside multicol)}

\x{%
Thirteenth line (inside multicol)}{%
Next to thirteenth line (inside multicol)}

}

\end{document}

答案2

您可以分两个阶段完成。首先创建一个文档 (test5.pdf) 以适合 tcolorbox 的内部。然后将每一页复制到 es 中tcolorbox以形成最终文档。

\documentclass{article}
\usepackage[a4paper, total={7in, 2in}]{geometry}
\usepackage{tcolorbox}
\newcounter{box}
\begin{document}

\begin{tcolorbox}[height=\textheight]
  text width \tcbtextwidth\par
  text height \tcbtextheight\par
\end{tcolorbox}% used to determine page size


\loop\stepcounter{box}%
  \tcbset{sharp corners}% default
  \ifnum\value{box}=1\relax \tcbset{rounded corners=north}\fi% first page
  \ifnum\value{box}=3\relax \tcbset{rounded corners=south}\fi% last page
  \begin{tcolorbox}
  \includegraphics[page=\thebox]{test5.pdf}%
  \end{tcolorbox}
  \ifnum\value{box}<3\relax\repeat% 3 pages

\end{document}

使用哪里test5.pdf创建的

\documentclass{article}
\usepackage[paperwidth=474.59206pt,paperheight=124.62315pt,margin=0pt]{geometry}
\usepackage{multicol}
\usepackage{lipsum}
\pagestyle{empty}
\begin{document}
\begin{multicols}{2}
\lipsum[1-4]
\end{multicols}
\end{document}

演示首页

答案3

我将其作为一个单独的答案,因为它使用了完全不同的方法(一个阶段)。它提供的环境提供了TwoColumnBox我之前解决方案的所有功能,并且它将使用第一页中剩余的任何空间(假设它已部分填充),并在最后一页上留下未使用的空间。

注意:我必须使用\parbox来设置列宽,因此可能需要在环境内设置诸如\parindent和之类的内容。\parskip

\documentclass{article}
\usepackage[a4paper, total={7in, 2in},showframe]{geometry}
\usepackage{tcolorbox}
\usepackage{environ}
\usepackage{tikzpagenodes}% for remaning height
\usepackage{lipsum}

\newsavebox{\TCbox}
\newlength{\TCpageheight}
\newlength{\TCheight}
\newlength{\TCwidth}
\newif\ifTCfirstpage

\NewEnviron{TwoColumnBox}[1][]% #1=tcolorbox options (optional)
{\let\columnbreak=\pagebreak
% get remaining height on page
  \tikz[overlay,remember picture]{%
    \pgfextracty{\TCpageheight}{\pgfpointanchor{current page text area}{south}}%
    \global\TCpageheight=\dimexpr-\TCpageheight-0.5\baselineskip\relax}%
  \sbox0{\begin{tcolorbox}[height=\TCpageheight,#1]
    \global\TCheight=\tcbtextheight
    \global\TCwidth=\tcbtextwidth
  \end{tcolorbox}}% hidden
  \ifdim\TCheight<\baselineskip \TCpageheight=\textheight \fi
% create source column
  \TCwidth=\dimexpr 0.5\TCwidth-0.5\columnsep\relax
  {\hsize=\TCwidth
    \global\setbox\TCbox=\vbox{\strut\BODY}}%
% break column into pages
  \TCfirstpagetrue
  \loop
    \sbox0{\begin{tcolorbox}[height=\TCpageheight,#1]
      \global\TCheight=\tcbtextheight
    \end{tcolorbox}}% hidden
    \tcbset{sharp corners}% default
    \ifTCfirstpage
      \TCfirstpagefalse
      \tcbset{rounded corners=north}%
      \TCpageheight=\textheight
    \fi
    \ifdim\ht\TCbox>2\TCheight
      \setbox0=\vsplit\TCbox to \TCheight
      \setbox1=\vsplit\TCbox to \TCheight
      \begin{tcolorbox}[#1]
        \hbox{\box0\hspace{\columnsep}\box1}
      \end{tcolorbox}
    \else
      \TCheight=0.5\ht\TCbox
      \setbox2=\copy\TCbox
      \setbox0=\vsplit\TCbox to \TCheight
      \ifdim\ht\TCbox>\TCheight
        \TCheight=\ht\TCbox
        \setbox\TCbox=\copy2
        \setbox0=\vsplit\TCbox to \TCheight
      \fi
      \setbox1=\vsplit\TCbox to \TCheight
      \begin{tcolorbox}[rounded corners=south,#1]
        \hbox{\box0\hspace{\columnsep}\box1}
      \end{tcolorbox}
    \fi
  \ifdim\ht\TCbox>0pt \repeat
}

\begin{document}
test line

\begin{TwoColumnBox}
  \lipsum[1-4]
\end{TwoColumnBox}
\end{document}

相关内容