我无法让铸造的环境适应线宽:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{minted}
\begin{document}
\begin{listing}
\begin{minted}{python}
from foo import Foo, Fuzz, Whizz, Buzz, Bar, Baz, Fee, Fie, Foe, Foo, Foobar, WhizzBuzz, LongBar, FieFoe, FeeFie, FooFuzz
a = Foo()
b = Bar()
c = a + b
print(c)
\end{minted}
\caption{Code example}
\end{listing}
\end{document}
我尝试了minipages
、、resizebox
并scalebox
玩弄字体大小。
使用 minipages 时,{\linewidth}
会忽略,并且最长的行会超出限制;resizebox/scalebox
使用 时会出错;使用 fontsizes 时,即使使用 ,第一行仍然放不下\tiny
。
我猜测 minted 和其他环境之间发生了一些奇怪的互动?
答案1
您可以将选项传递breaklines
给minted
环境,或使用以下命令全局设置该选项\setminted{breaklines}
:
\documentclass{article}
%\usepackage[utf8]{inputenc} % not really necessary since 2018
\usepackage{minted}
\setminted[python]{breaklines}
\begin{document}
\begin{listing}
% or \begin{minted}[breaklines]{python} if you need/want it only once
\begin{minted}{python}
from foo import Foo, Fuzz, Whizz, Buzz, Bar, Baz, Fee, Fie, Foe, Foo, Foobar, WhizzBuzz, LongBar, FieFoe, FeeFie, FooFuzz
a = Foo()
b = Bar()
c = a + b
print(c)
\end{minted}
\caption{Code example}
\end{listing}
\end{document}
答案2
Campa 的答案要好得多,但如果你真的不想断线,你可以把字体弄得比\tiny
这更小:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{minted}
\usepackage{lipsum}
\begin{document}
\begin{listing}
\begin{minted}[fontsize={\fontsize{5.5}{6.5}\selectfont}]{python}
from foo import Foo, Fuzz, Whizz, Buzz, Bar, Baz, Fee, Fie, Foe, Foo, Foobar, WhizzBuzz, LongBar, FieFoe, FeeFie, FooFuzz
a = Foo()
b = Bar()
c = a + b
print(c)
\end{minted}
\caption{Code example}
\end{listing}
\end{document}
(后面第一个数字\fontsize
设置字体大小;第二个数字设置行距。)
答案3
您可以调整minted
环境大小,但您会得到非常小的字形,这可能对读者没有帮助。
但是,当超调量不是很大时,以下解决方案可能会令人满意。否则,将出现如下所示的断线坎帕是一个更好的策略。
\documentclass{article}
\usepackage{minted}
\usepackage{graphicx}
\newsavebox{\mintedbox}
\begin{document}
\begin{listing}
\begin{lrbox}{\mintedbox}
% coerce minted to produce a box, rather than a full width environment
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}
\begin{minted}{python}
from foo import Foo, Fuzz, Whizz, Buzz, Bar, Baz, Fee, Fie, Foe, Foo, Foobar, WhizzBuzz, LongBar, FieFoe, FeeFie, FooFuzz
a = Foo()
b = Bar()
c = a + b
print(c)
\end{minted}
\end{lrbox}
\resizebox{\textwidth}{!}{\usebox{\mintedbox}}
\caption{Code example}
\end{listing}
\end{document}
诀窍是从包minted
内部使用,并且可以在本地重新定制它(请参阅文档以了解区别)。Verbatim
fancyvrb
BVerbatim
fancyvrb