\verb 与 \texttt 的组合

\verb 与 \texttt 的组合

我还读到:何时应使用 \verb,何时应使用 \texttt

我手头的问题是,我想编写一些程序代码,而不仅仅是“小片段”(链接问题中的措辞),但不必转义所有特殊字符。另一方面,\verb没有{}-style 分隔符,此外据说移动参数时会出现问题。

我能做些什么吗(示例文本如下)

“作为用户,请执行\texttt{if (!(last\_cond \&\& (last\_flags \& (O\_CREATE | O\_EXCL))))},但作为 root,请执行\texttt{if (!(last\_cond \&\& (last\_flags \& (O\_RDONLY))))}直到...”

可以写成

“作为用户,请执行\texttt{if (!(last_cond && (last_flags & (O_CREATE | O_EXCL))))},但作为 root,请执行\texttt{if (!(last_cond && (last_flags & (O_RDONLY))))}直到...”

编辑:定义一个新命令,如\verbdef并必须写“作为用户,执行\verbdef\demo1{\texttt{if (!(last_cond && (last_flags & (O_CREATE | O_EXCL))))}}\demo1,但作为 root,执行\verbdef\demo2{\texttt{if (!(last_cond && (last_flags & (O_RDONLY))))}}\demo2直到......”读起来很奇怪(在 tex 源文件中)。

答案1

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\small}
\begin{document}
foo
\lstinline{if (!(last_cond && (last_flags & (O_CREATE | O_EXCL))))}
bar
\end{document}

答案2

使用动词定义包裹

\documentclass{article}
\usepackage{verbdef}
\begin{document}
\verbdef\demo{if (!(last_cond && (last_flags & (O_CREATE | O_EXCL))))}
\demo
\end{document}

相关内容