我想显示类似如下的代码:
\begin{tikzpicture}[scale=0.2]
\draw[fill=blue!25] (-15,0) -- (-3,0) -- (-3,-12) -- (-15,-12) -- cycle;
\draw[fill=blue!25] (8,1) -- (14,1) -- (14, -5) -- (8, -5) -- cycle;
\draw[fill=blue!25] (16,1) -- (22,1) -- (22,-5) -- (16,-5) --cycle;
\draw[fill=blue!25] (8,-7) -- (14,-7) -- (14, -13) -- (8, -13) -- cycle;
\draw[fill=blue!25] (16,-7) -- (22,-7) -- (22,-13) -- (16,-13) --cycle;
\end{tikzpicture}
我已经找到了命令 \begin{lstlisting}[language={[latex]TeX}],但是这种方法会保留包特定的命令(如 \draw),而不会将它们变为粗体。
有什么想法可以实现吗?谢谢!
答案1
您可以添加lstlisting
使用该选项的关键字morekeywords={}
,并以逗号分隔关键字。
或者,您可以使用该minted
包。
两种选择的 MWE:
\documentclass{article}
\usepackage{listings}
\usepackage{minted}
\begin{document}
\begin{lstlisting}[language={[latex]TeX},morekeywords={draw}]
\begin{tikzpicture}[scale=0.2]
\draw[fill=blue!25] (-15,0) -- (-3,0) -- (-3,-12) -- (-15,-12) -- cycle;
\draw[fill=blue!25] (8,1) -- (14,1) -- (14, -5) -- (8, -5) -- cycle;
\draw[fill=blue!25] (16,1) -- (22,1) -- (22,-5) -- (16,-5) --cycle;
\draw[fill=blue!25] (8,-7) -- (14,-7) -- (14, -13) -- (8, -13) -- cycle;
\draw[fill=blue!25] (16,-7) -- (22,-7) -- (22,-13) -- (16,-13) --cycle;
\end{tikzpicture}
\end{lstlisting}
\begin{minted}{latex}
\begin{tikzpicture}[scale=0.2]
\draw[fill=blue!25] (-15,0) -- (-3,0) -- (-3,-12) -- (-15,-12) -- cycle;
\draw[fill=blue!25] (8,1) -- (14,1) -- (14, -5) -- (8, -5) -- cycle;
\draw[fill=blue!25] (16,1) -- (22,1) -- (22,-5) -- (16,-5) --cycle;
\draw[fill=blue!25] (8,-7) -- (14,-7) -- (14, -13) -- (8, -13) -- cycle;
\draw[fill=blue!25] (16,-7) -- (22,-7) -- (22,-13) -- (16,-13) --cycle;
\end{tikzpicture}
\end{minted}
\end{document}
结果: