我正在提交一个涉及代码块的项目。
我想到了一个好的方式来呈现它,那就是用 Courier 字体在浅灰色背景上显示黑色代码段落。
我想知道是否有可能创建一个名为“代码”的环境,例如,
\begin{code}
...
...
\end{code}
将具有灰色背景并采用 Courier 字体。
任何想法都将不胜感激!
答案1
要使用快递字体,
\renewcommand\ttdefault{pcr}
要定义code
环境,您可以使用listings
包。
\documentclass{book}
%\usepackage[scaled=O.85]{luximono}
\usepackage{xcolor,listings}
\definecolor{theblue} {rgb}{0.02,0.04,0.48}
\definecolor{thered} {rgb}{0.65,0.04,0.07}
\definecolor{thegreen}{rgb}{0.06,0.44,0.08}
\definecolor{thegrey} {gray}{0.5}
\definecolor{theshade}{gray}{0.94}
\definecolor{theframe}{gray}{0.75}
\renewcommand\ttdefault{pcr}
\lstloadlanguages{[LaTeX]TeX, [primitive]TeX}
\lstnewenvironment{code}[1][]
{\lstset{language=[LaTeX]TeX}\lstset{%
escapeinside={{(*@}{@*)}},
breaklines=true,
framesep=5pt,
basicstyle=\ttfamily,
showstringspaces=false,
keywordstyle=\ttfamily\textcolor{thegreen},
stringstyle=\color{orange},
commentstyle=\color{black},
rulecolor=\color{gray!10},
breakatwhitespace=true,
showspaces=false, % shows spacing symbol
xleftmargin=0pt,
xrightmargin=5pt,
aboveskip=0pt, % compact the code looks ugly in type
belowskip=0pt, % user responsible to insert any skips
backgroundcolor=\color{gray!15}, #1
}}
{}
\begin{document}
\begin{code}
\let a=2+3
\end{code}
\end{document}