UTF-8 Mu 无法在自定义环境中渲染

UTF-8 Mu 无法在自定义环境中渲染

我刚刚尝试使用这个https://tex.stackexchange.com/a/83883/143861用于在 overleaf 中高亮 Python 语法

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
morekeywords={self},              % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
showstringspaces=false
}}


% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

% Python for external files
\newcommand\pythonexternal[2][]{{
\pythonstyle
\lstinputlisting[#1]{#2}}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

Python 环境中的 Mu µ 无法渲染

\begin{document}
\begin{python}
µ = 5
\end{python}
\end{document}

由于此错误:

包 inputenc 错误:无效的 UTF-8 字节“B5。

它在 Python 环境之外进行渲染。

如何在语法高亮的 Python 代码块中渲染 mu?

答案1

您需要向 声明该角色listings

我还建议使用比newtxtt具有更好的符号覆盖率的txtt

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{newtxtt}

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{%
  \lstset{
    language=Python,
    basicstyle=\ttfamily,
    morekeywords={self},              % Add keywords here
    keywordstyle=\bfseries\color{deepblue},
    emph={MyClass,__init__},          % Custom highlighting
    emphstyle=\bfseries\color{deepred},    % Custom highlighting style
    stringstyle=\color{deepgreen},
    frame=tb,                         % Any extra options here
    showstringspaces=false,
  }%
}

\lstset{literate={µ}{\textmu}{1}}


% Python environment
\lstnewenvironment{python}[1][]
  {\pythonstyle\lstset{#1}}
  {}

% Python for external files
\newcommand\pythonexternal[2][]{{%
  \pythonstyle
  \lstinputlisting[#1]{#2}%
}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

\begin{document}
\begin{python}
µ = 5
for f
\end{python}
\end{document}

图片显示有一个独特的粗体字体。

在此处输入图片描述

相关内容