我的软件会自动创建 LaTeX 文档。我需要显示一些我无法控制的文本(由用户提供),因此它可能包含有问题的字符(如 _ 或 \ 或 $ 等),这些字符会破坏编译。
我可以查找每个特殊字符,然后将它们全部转义,然后再将它们插入 LaTeX 文档,但我需要一个详尽的列表,而且它对任何东西都不起作用(例如,反斜杠需要转换为\textbackslash
)。那么,没有 LaTeX 解决方案吗?
我认为也许verbatim
会有帮助,但我不确定。我需要将这个“受保护”的文本放在内联中(因此,使用\begin{verbatim}...\end{verbatim}
不起作用),并使用与周围文本相同的字体。
我以为使用fancyvrb
这样的功能:\Verb[fontfamily=Ubuntu]|...|
可以解决问题,但事实并非如此。
使用以下代码(使用 lualatex 编译):
\RequirePackage{luatex85}
\documentclass[a4paper, fleqn, 12pt]{article}
\usepackage{lxfonts}
\usepackage{amsmath}
\usepackage{fancyvrb}
\usepackage[no-math]{fontspec}
\AtEndPreamble{\setmainfont{Ubuntu}[NFSSFamily=fontid]}
\DeclareSymbolFont{mynumbers} {TU}{fontid}{m}{n}
\SetSymbolFont {mynumbers}{bold}{TU}{fontid}{bx}{n}
\AtBeginDocument{
\DeclareMathSymbol{0}{\mathalpha}{mynumbers}{`0}
\DeclareMathSymbol{1}{\mathalpha}{mynumbers}{`1}
\DeclareMathSymbol{2}{\mathalpha}{mynumbers}{`2}
\DeclareMathSymbol{3}{\mathalpha}{mynumbers}{`3}
\DeclareMathSymbol{4}{\mathalpha}{mynumbers}{`4}
\DeclareMathSymbol{5}{\mathalpha}{mynumbers}{`5}
\DeclareMathSymbol{6}{\mathalpha}{mynumbers}{`6}
\DeclareMathSymbol{7}{\mathalpha}{mynumbers}{`7}
\DeclareMathSymbol{8}{\mathalpha}{mynumbers}{`8}
\DeclareMathSymbol{9}{\mathalpha}{mynumbers}{`9}
\DeclareMathSymbol{.}{\mathalpha}{mynumbers}{`.}
\DeclareMathSymbol{,}{\mathalpha}{mynumbers}{`,}
}
\newfontfamily\configfont{Ubuntu}
\usepackage{geometry}
\geometry{hmargin=0.75cm, vmargin=0.75cm}
\begin{document}
Normal text using Ubuntu font\hfill \Verb[fontfamily=Ubuntu]|Short but uncontrolled text with possible $ _ \ é è à|
\end{document}
答案1
fontfamily
中的键接受\Verb
NFSS 家族名称。在你的情况下,这是fontid
,而不是 'Ubuntu (specified by the
NFSSFamily key to
\setmainfont')。然后你得到
\RequirePackage{luatex85}
\documentclass[a4paper, fleqn, 12pt]{article}
\usepackage{lxfonts}
\usepackage{amsmath}
\usepackage{fancyvrb}
\usepackage[no-math]{fontspec}
\AtEndPreamble{\setmainfont{Ubuntu}[NFSSFamily=fontid]}
\DeclareSymbolFont{mynumbers} {TU}{fontid}{m}{n}
\SetSymbolFont {mynumbers}{bold}{TU}{fontid}{bx}{n}
\AtBeginDocument{
\DeclareMathSymbol{0}{\mathalpha}{mynumbers}{`0}
\DeclareMathSymbol{1}{\mathalpha}{mynumbers}{`1}
\DeclareMathSymbol{2}{\mathalpha}{mynumbers}{`2}
\DeclareMathSymbol{3}{\mathalpha}{mynumbers}{`3}
\DeclareMathSymbol{4}{\mathalpha}{mynumbers}{`4}
\DeclareMathSymbol{5}{\mathalpha}{mynumbers}{`5}
\DeclareMathSymbol{6}{\mathalpha}{mynumbers}{`6}
\DeclareMathSymbol{7}{\mathalpha}{mynumbers}{`7}
\DeclareMathSymbol{8}{\mathalpha}{mynumbers}{`8}
\DeclareMathSymbol{9}{\mathalpha}{mynumbers}{`9}
\DeclareMathSymbol{.}{\mathalpha}{mynumbers}{`.}
\DeclareMathSymbol{,}{\mathalpha}{mynumbers}{`,}
}
\newfontfamily\configfont{Ubuntu}
\usepackage{geometry}
\geometry{hmargin=0.75cm, vmargin=0.75cm}
\begin{document}
Normal text using Ubuntu font\hfill \Verb[fontfamily=fontid]|Short but uncontrolled text with possible $ _ \ é è à|
\end{document}
答案2
fontid
在手册中仅用作路标,因此应该使用一个有意义的名称。
这里还有一些仅用于lxfonts
数学的建议,以避免它改变文本模式的一些重要默认值。
\documentclass[a4paper, fleqn, 12pt]{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{fancyvrb}
\usepackage{lxfonts}
\usepackage[no-math]{fontspec}
\geometry{hmargin=0.75cm, vmargin=0.75cm}
%%% fixes in order to use lxfonts only for math
\let\savedrmdefault\rmdefault
\let\savedsfdefault\sfdefault
\let\savedttdefault\itdefault
\let\saveditdefault\itdefault
\let\savedsldefault\sldefault
\let\savedbxdefault\bxdefault
\AtEndPreamble{% undo the nonmath settings by lxfonts
\let\rmdefault\savedrmdefault
\let\sfdefault\savedsfdefault
\let\ttdefault\saveditdefault
\let\itdefault\saveditdefault
\let\sldefault\savedsldefault
\let\bxdefault\savedbxdefault
\setmainfont{Ubuntu}[NFSSFamily=ubuntu]%
}
%%%
\DeclareSymbolFont{mynumbers}{TU}{ubuntu}{m}{n}
\SetSymbolFont{mynumbers}{bold}{TU}{ubuntu}{bx}{n}
\AtBeginDocument{%
\DeclareMathSymbol{0}{\mathalpha}{mynumbers}{`0}%
\DeclareMathSymbol{1}{\mathalpha}{mynumbers}{`1}%
\DeclareMathSymbol{2}{\mathalpha}{mynumbers}{`2}%
\DeclareMathSymbol{3}{\mathalpha}{mynumbers}{`3}%
\DeclareMathSymbol{4}{\mathalpha}{mynumbers}{`4}%
\DeclareMathSymbol{5}{\mathalpha}{mynumbers}{`5}%
\DeclareMathSymbol{6}{\mathalpha}{mynumbers}{`6}%
\DeclareMathSymbol{7}{\mathalpha}{mynumbers}{`7}%
\DeclareMathSymbol{8}{\mathalpha}{mynumbers}{`8}%
\DeclareMathSymbol{9}{\mathalpha}{mynumbers}{`9}%
\DeclareMathSymbol{.}{\mathord}{mynumbers}{`.}%
\DeclareMathSymbol{,}{\mathpunct}{mynumbers}{`,}%
}
\newfontfamily\configfont{Ubuntu}
\newcommand{\uverb}{\Verb[fontfamily=ubuntu]}
\begin{document}
Normal text using Ubuntu font
\uverb|Short but uncontrolled text with possible $ _ \ é è à|
$(a,b+123)$ 123
\end{document}
注意句号应该是\mathord
和逗号\mathpunct
。