我想创建一个新的环境来自动执行以下代码:
\setbox0=\hbox{\begin{minipage}{5.8in}
\color{black}
\begin{verbatim}
<Some verbatim text here>
\end{verbatim}
\end{minipage}}
\fcolorbox{blue!30}{gray!7}{\box0}
我对 Latex 还很陌生,所以如果这个问题不清楚或者有明显的错误,我深表歉意。我还想说这个网站极好的对于像我这样的新 Latex 用户来说非常有帮助。
答案1
改编自这个答案
\documentclass[12pt]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{fancyvrb,xcolor}
\usepackage{lipsum} % just for the example
\newsavebox{\FVerbBox}
\newenvironment{FVerbatim}
{\VerbatimEnvironment
\begin{center}
\begin{lrbox}{\FVerbBox}
\begin{minipage}{5.8in}
\begin{Verbatim}}
{\end{Verbatim}
\end{minipage}
\end{lrbox}
\fcolorbox{blue!30}{gray!7}{\usebox{\FVerbBox}}
\end{center}}
\begin{document}
\lipsum*[2]
\begin{FVerbatim}[fontsize=\small]
Text.
With { \ strange /##& characters
\end{FVerbatim}
\lipsum[3]
\end{document}
这是带有额外垂直空间的版本。与的交互Verbatim
使得猜测正确的比例有些困难,只需进行实验即可。
\documentclass[12pt]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{fancyvrb,xcolor}
\usepackage{lipsum} % just for the example
\newsavebox{\FVerbBox}
\newenvironment{FVerbatim}
{\VerbatimEnvironment
\begin{center}
\vspace{1ex} % additional space above the frame
\begin{lrbox}{\FVerbBox}
\begin{minipage}{5.8in}
\vspace*{4ex} % additional space before the verbatim material
\begin{Verbatim}}
{\end{Verbatim}
\vspace{1ex} % additional space after the verbatim material
\end{minipage}
\end{lrbox}
\fcolorbox{blue!30}{gray!7}{\usebox{\FVerbBox}}
\vspace{1ex} % additional space after the frame
\end{center}}
\begin{document}
\lipsum*[2]
\begin{FVerbatim}[fontsize=\small]
Text.
With { \ strange /##& characters
\end{FVerbatim}
\lipsum[3]
\end{document}
想要更精致的盒子,你可以看看tcolorbox