选项mathescape

选项mathescape

我正在使用 lyx 并收到错误消息,当我尝试以下示例时,° 符号被排除:

\usepackage{listings}
\begin{lstlisting}
10°C
\end{lstlisting}

编辑:正如所要求的,这里是来自 lyx 的乳胶代码,需要它的地方:

%% LyX 2.1.3 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{textcomp}
\usepackage{graphicx}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{~/Library/texmf/tex/latex/verbatimfiles}

\makeatother

\usepackage{babel}
\usepackage{listings}
\renewcommand{\lstlistingname}{Listing}

\begin{document}
\begin{itemize}
\item \emph{class variables}\\
\emph{}
\begin{lstlisting}
Public celsius() As Char = {" ", "°", "C", "/", "m", "i", "n"}
\end{lstlisting}
This variable is used to trim the output of Linkam functions, so numerical
value operation\emph{ }are possible with the outputs. It is made public,
so it can be accessed by functions outside this class, where the trimming
is nescessary.\\

\end{itemize}

\end{document}

答案1

您的意思是?启用${^\circ}$选项后,这将为您提供度数符号。mathescape

你的脚本看起来会像这样:

\documentclass{article}
\usepackage{listings}
\begin{lstlisting}[mathescape]
10${^\circ}$C
\end{lstlisting}

在此处输入图片描述

答案2

我认为问题是由于 引起的\usepackage[utf8]{inputenc}。只有当字体编码包含该字符时,Unicode 字符才受支持。否则包将不知道如何打印该字符。可以使用由包 加载的inputenc编码。TS1textcomp

下一个障碍是 ackagelistings不支持多字节字符。

有一些解决方法:

选项mathescape

软件包listings提供了逃逸机制,其中之一是mathescape,参见回答Anne 的。数学内容不会被解析器解析和拆分。包的listings宏(或)会转回文本模式:\textamstextamsmath

\documentclass{article}
\usepackage{listings}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}% TS1 encoding for the degree sign
\usepackage{amstext}
\begin{document}
\begin{lstlisting}[mathescape]
10$\text{°}$C
\end{lstlisting}
\end{document}

此版本对于灵活的列布局很有用。

结果

选项literate

literate功能用正确的符号替换标记(度数符号的 UTF-8 字节序列):

\documentclass{article}
\usepackage{listings}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}% TS1 encoding for the degree sign
\begin{document}
\begin{lstlisting}[literate={°}{\textdegree}1]
10°C
\end{lstlisting}
\end{document}

对于固定列布局有用,1表示替换一个字符的位置。

结果识字

包裹listingsutf8

如果此清单的字符适合已知的 8 位编码,则包listingsutf8会提供帮助,它在读取时重新编码文件\listinputlisting。环境lstlistings\lstinline不受支持。包filecontents有助于将清单放入文件中:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listingsutf8}
\usepackage{textcomp}% TS1 encoding for the degree sign
\usepackage{filecontents}
\begin{document}

\begin{filecontents*}{\jobname-lst-degree}
10°C
\end{filecontents*}

\lstinputlisting[inputencoding=utf8/latin1]{\jobname-lst-degree}
\end{document}

结果

答案3

我们可以在 lyx 中添加度数圈:

在文本中,依次选择“插入”→“特殊字符”→“符号...”→“Latin-1 Supplement”并点击“度数圈”,在 Latin-1 Supplement 类别中有 2 种大小的度数圈。

在公式中,从“数学面板”单击“杂项”,然后单击“textdegree”。

相关内容