我希望能够自动将 TeX 特殊符号打印到 pdf 中。主要是(但不仅限于)下划线符号_
,但也用灰色突出显示(类似于它在此站点中的显示方式)。原因:我正在为同事编写基本的 python 和 R 教程。我经常使用 pythonTeX。但我经常必须编写包含下划线的代码。转义很繁琐且容易出错,我希望找到自动化解决方案。
这是此问题的后续问题:
fvextra 中的 \Verb{}:突出显示 \Verb{} 文本,以便能够在行末换行
Poore 先生在那里给了我一些指导,但由于我对 TeX 还不熟悉,所以我并不完全清楚如何实现我的目标。
梅威瑟:
\documentclass [a4paper, 12pt, twoside, openright] {scrbook}
\usepackage{fontspec}
\usepackage [left=2.5cm, right=2cm, bottom=3cm, headheight=15.3pt] {geometry}
\usepackage[dvipsnames,x11names,svgnames,table]{xcolor}
\usepackage{fvextra}
\fvinlineset{breaklines,%
breakafter=\space ,
breakanywhere
}
\usepackage[htt]{hyphenat}
\usepackage{soul}
\usepackage{soulutf8}
\sethlcolor{Snow2}
\newcommand{\hltt}[1]{%
\hl{\texttt{#1}}
}
\newcommand{\hlttlua}[1]{%
\hl{\texttt{\directlua{tex.print(-2,"#1")}}}%
}
\begin{document}
\begin{enumerate}
\item Another option with use of \EscVerb{\\directlua} with more special symbols: \hltt{Hello W\_o\_r\#ld without percent sign}
\item Another option with use of \EscVerb{\\directlua} with more special symbols: \directlua{tex.print(-2,"Hello W_o_r#ld without percent sign")}
\item Another option with use of \EscVerb{\\directlua} with more special symbols: \texttt{\directlua{tex.print(-2,"Hello W_o_rld without percent sign")}}
\item Another option with use of \EscVerb{\\directlua} with more special symbols: \hlttlua{Hello W_o_rld without percent sign}
\end{enumerate}
\end{document}
解释:希望使 4 号工作。现在,我收到错误:
unexpected symbol near '{'
我不知道该如何处理(而且我在 SE 中也找不到解决方案)。pdf 中所需的外观与第 1 种相同;但没有在源中进行手动转义。
答案1
您想在进入场景\directlua
之前进行扩展。\hl
\documentclass [a4paper, 12pt, twoside, openright] {scrbook}
\usepackage{fontspec}
\usepackage [left=2.5cm, right=2cm, bottom=3cm, headheight=15.3pt] {geometry}
\usepackage[dvipsnames,x11names,svgnames,table]{xcolor}
\usepackage{fvextra}
\fvinlineset{breaklines,
breakafter=\space ,
breakanywhere
}
\usepackage[htt]{hyphenat}
\usepackage{soul}
\usepackage{soulutf8}
\sethlcolor{Snow2}
\newcommand{\hltt}[1]{%
\hl{\texttt{#1}}%
}
\newcommand{\hlttlua}[1]{%
\expanded{\noexpand\hl{\noexpand\texttt{\directlua{tex.print(-2,"#1")}}}}%
}
\begin{document}
\begin{enumerate}
\item Another option with use of \EscVerb{\\directlua} with more special symbols:
\hltt{Hello W\_o\_r\#ld without percent sign}
\item Another option with use of \EscVerb{\\directlua} with more special symbols:
\directlua{tex.print(-2,"Hello W_o_r#ld without percent sign")}
\item Another option with use of \EscVerb{\\directlua} with more special symbols:
\texttt{\directlua{tex.print(-2,"Hello W_o_rld without percent sign")}}
\item Another option with use of \EscVerb{\\directlua} with more special symbols:
\hlttlua{Hello W_o_rld without percent sign}
\end{enumerate}
\end{document}