我已经定义了我的新命令:
% sets for lstlisting
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
frame=single,
rulecolor=\color{colorCodeBorder}
}
\newcommand{\mynote}[1]{
\begin{tcolorbox}[colframe=black!50]
{#1}
\end{tcolorbox}
}
并尝试将其与 lstlisting 一起使用
\mynote{
always use this command:
\begin{lstlisting}
some code
\end{lstlisting}
}
但出现错误:
Package Listings Warning: Text dropped after begin of listing on input line ...
怎么解决呢?
编辑:如果我跳过我的命令那么就可以了:
\begin{tcolorbox}[colframe=black!20]
Always use this code:
\begin{lstlisting}
some code
\end{lstlisting}
\end{tcolorbox}
但由于其他问题,我需要通过 \mynote 来调用它...... 那么我在定义命令时做错了吗? 感谢您的任何帮助。
答案1
您不能方便地将\verb
类似代码(例如列表)放入宏参数中。请参阅这里以获得解释。但是你可以使用环境来代替,并且\newtcblisting
来自的命令tcolorbox
对定义环境很有帮助:
\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listingsutf8} % or 'listings' if you use XeTeX
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
}
\newtcblisting{mynotesidebyside}[1]{listing only, colframe=black!50,
comment side listing, comment={#1}}
\newtcblisting{mynoteinside}[1]{listing only, colframe=black!50,
comment and listing, comment={#1}}
\newtcblisting{mynoteabove}[1]{listing only, colframe=black!50,
comment above listing, comment={#1}}
\begin{document}
\begin{mynotesidebyside}{Always use this code:}
a = (b < c) ? d : e;
f = &a;
\end{mynotesidebyside}
\begin{mynoteinside}{Always use this code:}
a = (b < c) ? d : e;
f = &a;
\end{mynoteinside}
\begin{mynoteabove}{Always use this code:}
a = (b < c) ? d : e;
f = &a;
\end{mynoteabove}
\end{document}