有没有办法修改minted
用于绘制背景的代码,使其位于页面中间或自定义长度?我认为应该使用\newcommand
或类似的东西来完成,因为我想为大段代码保留默认样式,为小段代码保留自定义背景长度。
\documentclass{article}
\usepackage{minted}
\usemintedstyle[c++]{manni}
\usepackage{xcolor}
\definecolor{codebackground}{RGB}{240, 240, 235}
\newlength{\msize}
\setlength{\msize}{\dimexpr(\paperwidth-\textwidth)/2\relax}
\begin{document}
\hspace*{\msize} Stop background here?\par
\hspace*{\msize} $ \downarrow $
\begin{minted}[bgcolor=codebackground]{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{document}
以下是使用的代码minted
,取自其文档
\newenvironment{minted@colorbg}[1]{%
\setlength{\OuterFrameSep}{0pt}%
\colorlet{shadecolor}{#1}%
\let\minted@tmp\FV@NumberSep
\edef\FV@NumberSep{%
\the\numexpr\dimexpr\minted@tmp+\number\fboxsep\relax sp\relax}%
\medskip
\begin{snugshade*}}
{\end{snugshade*}%
\medskip\noindent}
答案1
如果你想将minted
环境装箱,你可以这样做
\documentclass{article}
\usepackage{minted}
\usepackage{xcolor}
\usemintedstyle[c++]{manni}
\definecolor{codebackground}{RGB}{240, 240, 235}
\newsavebox{\mintedbox}
\newenvironment{boxminted}
{%
\VerbatimEnvironment
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
\begin{lrbox}{\mintedbox}
\begin{minted}%
}
{%
\end{minted}%
\end{lrbox}%
\noindent\colorbox{codebackground}{\usebox{\mintedbox}}%
}
\begin{document}
\begin{boxminted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{boxminted}
\end{document}
对于固定尺寸的宽度,
\documentclass[a4paper]{article}
\usepackage{minted}
\usepackage{xcolor}
\usemintedstyle[c++]{manni}
\definecolor{codebackground}{RGB}{240, 240, 235}
\newlength{\msize}
\setlength{\msize}{\dimexpr\textwidth/2\relax}
\newsavebox{\mintedbox}
\newenvironment{boxminted}
{%
\VerbatimEnvironment
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
\begin{lrbox}{\mintedbox}
\begin{minted}%
}
{%
\end{minted}%
\end{lrbox}%
\noindent\colorbox{codebackground}{\makebox[\msize][l]{\usebox{\mintedbox}}}%
}
\begin{document}
\begin{boxminted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{boxminted}
\end{document}