我的代码如下:
\documentclass{book}
\usepackage{xcolor,mdframed}
\usepackage{showframe}
\begin{document}
\setlength{\marginparwidth}{2pc}
\newenvironment{codebox}{%
\color{blue}%
}{}%
\surroundwithmdframed[
skipabove=\baselineskip,
hidealllines=true,leftmargin=-24pt,innerleftmargin=-24pt,
backgroundcolor=black!10,
]{codebox}
\begin{codebox}
All code snippets %\marginpar{MS:74}
in the book are set aside and appear like this. They
will show some minimal code for purposes of illustration. For example,
this is how to compute the cosine of a sequence of numbers:
\begin{verbatim}
>>> import numpy as np
>>> x = np.random.random(100) # 100 numbers between 0 and 1
>>> cos_x = np.cos(x) # cosine of each element.
\end{verbatim}
For more details on the essential modules for scientific computing
in Python, see appendix~\ref{app:python_tutorial}.
\end{codebox}
\end{document}
我得到的输出为:
我需要将灰色阴影延伸至文本的最左侧,可以吗?请指教...
答案1
把leftmargin
和innerleftmargin
放到0pt
,问题就解决了。我希望能够解决您的问题,尤其是能够理解您的请求。抱歉我的英语一直不好。
\documentclass{book}
\usepackage{xcolor,mdframed}
\usepackage{showframe}
\begin{document}
\setlength{\marginparwidth}{2pc}
\newenvironment{codebox}{%
\color{blue}%
}{}%
\surroundwithmdframed[
skipabove=\baselineskip,
hidealllines=true,
leftmargin=0pt,
innerleftmargin=0pt,
backgroundcolor=black!10,
]{codebox}
\begin{codebox}
All code snippets %\marginpar{MS:74}
in the book are set aside and appear like this. They
will show some minimal code for purposes of illustration. For example,
this is how to compute the cosine of a sequence of numbers:
\begin{verbatim}
>>> import numpy as np
>>> x = np.random.random(100) # 100 numbers between 0 and 1
>>> cos_x = np.cos(x) # cosine of each element.
\end{verbatim}
For more details on the essential modules for scientific computing
in Python, see appendix\ldots.
\end{codebox}
\end{document}
如果要将整个东西向左移动,请添加几何包,即\usepackage[left=.5in]{geometry}
。