我需要將程式碼清單中的一些文字加粗,為此我正在使用以下程式碼,該程式碼在 lstlisting 中使用 mathescape=true 和 \textbf:
\begin{lstlisting}[language={}, basicstyle={\small\ttfamily},mathescape=true]
regular text
$\textbf{hello hi}$
regular text
\end{lstlisting}
這很好用,因為它以粗體文字顯示 hello hi。但是當 hello 和 hi 之間有換行符時,如下所示:
$\textbf{hello
hi}$
結果中不存在換行符。我嘗試添加 \ 但也不起作用。
$\textbf{hello \\
hi}$
知道如何在這裡添加換行符嗎?
答案1
你正在逃避數學,也$\textbf{hello\\hi}$
不會在清單之外工作。使用escapechar=$
而不是mathescape=true
:
\documentclass{article}
\usepackage{listings}
\begin{document}
$\textbf{hello\\hi}$
\begin{lstlisting}[language={}, basicstyle={\small\ttfamily},escapechar=$]
regular text
$\textbf{hello\\hi}$
regular text
\end{lstlisting}
\end{document}
(它不是粗體,因為您沒有提供顯示字體設定的完整範例。)我