根据文本宽度增加框架框宽度

根据文本宽度增加框架框宽度

如果框超出 \textwidth(当前为 300pt),则框架框需要增加 350pt 宽度,在上述定义的环境中,我尝试了很多方法都没有成功,您能尝试解决我的问题吗?注意:解决方案需要现有的宏。

\documentclass[draft]{book}
\usepackage{color,alltt,lipsum}
\overfullrule0pt

\setlength\textwidth{300bp}%
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}

\usepackage{framed}
\makeatletter
\newbox\kframebox%
\newdimen\kframeboxwd%

\newenvironment{kframe}{%
\fboxsep3.5pt%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin %\hskip-\fboxsep
  \colorbox{shadecolor}{##1}%\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}
   }%
 {\par\unskip\endMakeFramed%\vskip.5pt
 \at@end@of@kframe%
 }

\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

%\newenvironment{kframelong}{\bgroup\par%
%\hsize428bp\fboxsep3.5pt%%
% \def\at@end@of@kframe{}%
% \ifinner\ifhmode%
%  \def\at@end@of@kframe{\end{minipage}}%
%  \begin{minipage}{\columnwidth}%
% \fi\fi%
% \def\FrameCommand##1{\hskip\@totalleftmargin %\hskip-\fboxsep
%  \colorbox{shadecolor}{##1}%\hskip-\fboxsep
%    \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
% \MakeFramed {\advance\hsize-\width
%   \@totalleftmargin\z@ \linewidth\hsize
%   \@setminipage}
%   }%
% {\par\unskip\endMakeFramed%\vskip.5pt
% \at@end@of@kframe%
% \egroup\par%
% %
% }


\makeatother
\begin{document}

\lipsum[5]

\definecolor{shadecolor}{cmyk}{0,0.05,0.14,0}\color{fgcolor}
\begin{kframe}
\begin{alltt}
\textcolor{blue}{17}\textcolor{red}{:}\textcolor{blue}{58}
\end{alltt}
\begin{verbatim}
##  [1] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
## [23] 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
\end{verbatim}
\end{kframe}

\lipsum[5]

\begin{kframe}
\begin{alltt}
\textcolor{blue}{17}\textcolor{red}{:}\textcolor{blue}{58}
\end{alltt}
\begin{verbatim}
##  [1] 17 18 19 20 21 22 23 24 25
## [23] 39 40 41 42 43 44 45
\end{verbatim}
\end{kframe}

\end{document}

如果 Framebox 超出 \textwidth,则 Framebox 需要增加 350pt 宽度

在此处输入图片描述

答案1

这看起来像是试图修复糟糕的 R/knitr 输出。在这种情况下,你走错了路。否则,这个答案可能不是解决方案,但请考虑 knitr 产生相同输出的简单性。

\documentclass{book}
\usepackage{lipsum}
\begin{document}
<<options,echo=FALSE>>=
    options(width = 60)
@
\lipsum[5][1-5]
<<seq,background=c(1,.95,.86)>>=
    17:58
@
\lipsum[6][1-4]
\end{document}

姆韦

相关内容