带有表格的包装盒有多余的垂直空间

带有表格的包装盒有多余的垂直空间
\begin{wrapfigure}{r}{0in}
\begin{tabular}{|p{2in}|}
\hline
\begin{verbatim}
Some stuff here, hi mom
abcdefghijklmno
\end{verbatim} \\
\hline
\end{tabular}
\end{wrapfigure}

这并不能完全满足我的要求,因为框最终会在文本的顶行和框顶部之间留出多余的垂直空间。我绝对想要逐字文本,并且能够将文本环绕在框外面。

答案1

虽然需要付出更多开销,但tcolorbox与使用表格来“构建”逐字文本相比,它能让您更好地控制。此外,您可以更加喜欢这些逐字文本小片段的构建方式。例如:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{wrapfig,tcolorbox}

% tcolorbox
\newenvironment{myverbatim}{%
  \begingroup\tcbverbatimwrite{\jobname.ex.txt}}%
{\endtcbverbatimwrite\endgroup}

% wrapfig.sty
\setlength{\intextsep}{0pt}

\begin{document}

This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box.
This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box. XXX
\begin{myverbatim}
\begin{verbatim}
Some stuff here, hi mom
abcdefghijklmno `\verb'
\end{verbatim}
\end{myverbatim}
\begin{wrapfigure}[4]{r}{0in}
 \begin{tcolorbox}[nobeforeafter,width=2in,colback=red!50!white] % <-- purposely odd colour chosen for 'colback'...
   \input{\jobname.ex.txt}
 \end{tcolorbox}
\end{wrapfigure}
This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box.
This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box.


\end{document}

如果更简单的解决方案更有吸引力,您可以局部将\topsep\FrameSep和更改\intextsep为零(或某个小值)。该包etoolbox可以帮助保持整洁:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{wrapfig,framed,etoolbox}

% wrapfig
\setlength{\intextsep}{0pt}
% framed
\setlength{\FrameSep}{2pt}
% etoolbox
\AtBeginEnvironment{wrapfigure}{\setlength{\topsep}{0pt}}

\begin{document}

This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box.
This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box.
\begin{wrapfigure}[3]{r}{2in}
%\setlength{\topsep}{0pt}
\begin{framed}
\begin{verbatim}
Some stuff here, hi mom
abcdefghijklmno
\end{verbatim}
\end{framed}
\end{wrapfigure}
This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box.
This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box. XXX
% Compare:
\begin{framed}
\begin{verbatim}
Some stuff here, hi mom
abcde
\end{verbatim}
\end{framed}
This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box.
This doesn't quite do what I want, as the box ends up with extraneous vertical space between the top line of the text and the top of the box. I definitely want verbatim text and the ability to wrap the text outside around the box. XXX

\end{document}

答案2

您可以使用其他verbatim提供软件包,例如fancyvrb

在此处输入图片描述

\documentclass{article}
\usepackage{lipsum,wrapfig,fancyvrb}
\begin{document}

\begin{wrapfigure}{r}{2in}
  \vspace*{-.5\baselineskip}% Vertical adjustment above
\begin{Verbatim}[frame=single,framerule=.4pt]
Some stuff here, hi mom
abcdefghijklmno
\end{Verbatim}
  \vspace*{-1.5\baselineskip}% Vertical adjustment below
\end{wrapfigure}
\lipsum[1]
\end{document}

可能需要进行上方/下方的垂直调整,但包装可以很好地完成框架。

相关内容