如何在彩色的 Python 代码列表中包含减号和连字符?

如何在彩色的 Python 代码列表中包含减号和连字符?

帖子:

如何在 LaTeX Listings \lstinputlistings 命令中突出显示 Python 语法

产生的答案来自红色模式,它给出了一个彩色的 Python 代码列表。尽管文本在此 Latex 代码中产生了减号/连字符。当我将它引入到我自己的代码中(如下)时,它没有。有人能帮我解决这个问题吗?我在 Ubuntu 20.04.1 LTS 上使用 TeXstudio 3.0.1。

----------------------- redmode 写道 ----------------------

好的方法是为编程语言定义新的环境。最低限度的设置可以围绕以下内容:

\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,
otherkeywords={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!}}

\begin{document}

\section{``In-text'' listing highlighting}

\begin{python}
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '-5 1 2 3 -4'
        -10
        print bla
\end{python}

%\section{External listing highlighting}

%\pythonexternal{demo.py}

\section{Inline highlighting}

Definition \pythoninline{class MyClass} means \dots

\end{document}

在此处输入图片描述

这是我的乳胶代码,其中放置了上述代码,但没有显示减号。

%\documentclass[12pt]{scrartcl}
%\documentclass[12pt]{amsart}
%\documentclass[12pt]{article}
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
%\documentclass{article}
\usepackage{geometry}
\usepackage{graphicx}
%\pagestyle{empty}
%\mathchardef\hyphenmathcode=\mathcode`\- % BJD added here 16.12.2020

\usepackage[version=4]{mhchem} % BJD added here 14.8.2018
\usepackage{breqn}  % BJD added here 14.8.2018

\usepackage[colorlinks=true, citecolor=blue, linkcolor=blue, urlcolor=blue]{hyperref}
\newgeometry{asymmetric, centering}
\usepackage [english]{babel} % BJD added here
\usepackage [autostyle, english = american]{csquotes} % BJD added here
%\usepackage[T1]{fontenc} % BJD 20.10.2018
%\usepackage[utf8]{inputenc} %BJD 20.10.2018
%\usepackage[font=small,labelfont=bf,tableposition=top]{caption} %BJD 20.10.2018
%\MakeOuterQuote{"} % BJD added here
%% show notes and keys in the draft mode %%%%%%%%%
\usepackage{ifdraft}
\ifoptionfinal{
\usepackage[disable]{todonotes}
}{
\usepackage[norefs, nocites]{refcheck}
\usepackage{soul}
%\providecommand*\showkeyslabelformat[1]{\normalfont\tiny\ttfamily#1} %BJD 20.2.2020
%\usepackage[notref, notcite]{showkeys} %BJD 20.2.2020
\usepackage[bordercolor=white, color=white]{todonotes}
}
\usepackage{xfrac}  % BJD added here 15.8.2018
\usepackage{amsmath, amsthm, amssymb, amsfonts} % BJD added here 23.8.2018 
\usepackage{upgreek} % BJD added here 25.8.2018 

\usepackage{rotating, graphicx} % BJD added here 20.10.2018
\usepackage{tikz} % BJD added here 20.10.2018
\usepackage{blindtext} % BJD added here 20.10.2018
\usepackage{caption} % BJD added here 20.10.2018
%\usepackage{tikz} % BJD added here 20.10.2018
\usepackage{hyperref}
\usepackage{xcolor}

%------------BJD 15.8.2018-----------------------
\makeatletter
\def\@settitle{\begin{center}%
        \baselineskip14\p@\relax
        \bfseries
        \uppercasenonmath\@title
        \@title
        \ifx\@subtitle\@empty\else
        \\[1ex]\uppercasenonmath\@subtitle
        \footnotesize\mdseries\@subtitle
        \fi
    \end{center}%
}
\def\subtitle#1{\gdef\@subtitle{#1}}
\def\@subtitle{}
\makeatother

%\usepackage{listings}
%\lstset{
%   defaultdialect=[Visual]Basic
%   ,frameround=fttt
%   ,language=SQL
%   ,numbers=left
%   ,breaklines=true
%   ,showstringspaces=false
%   ,basicstyle=\small
%}
%---------------BJD added 16.12.2020--------------------
% 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,
        otherkeywords={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!}}

%-------------------------------------------
% \let\origlstlisting=\lstlisting
%\let\endoriglstlisting=\endlstlisting
%\renewenvironment{lstlisting}
%{\mathcode`\-=\hyphenmathcode
%   \everymath{}\mathsurround=0pt\origlstlisting}
%{\endoriglstlisting}
%-------------------end 16.12.2020--------------------------------------


%-------------------------------------------------
%******BJD change****************
%\documentclass{article}
%\usepackage{graphicx}
%\graphicspath{ {/home/user/images/} }
%\graphicspath{ {/home/bjd/Downloads/} }
%******BJD change end****************
\newcommand{\HOX}[1]{\todo[noline, size=\footnotesize]{#1}}
\newcommand{\TODO}[1]{\hfill\todo[inline, bordercolor=black, color=green!10]{#1}}
%% HACK from Sec. 1.6.4 of the manual of todonotes package
\makeatletter\providecommand\@dotsep{5}\def\listtodoname{List of Todos}\def\listoftodos{\hypersetup{linkcolor=black}\@starttoc{tdo}\listtodoname\hypersetup{linkcolor=blue}}\makeatother
%%%%%%%%%%%%%%%%%%

%% numbering %%%%%%%%%
\newtheorem{lemma}{Lemma}
\newtheorem{proposition}{Proposition}
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}{Corollary}
\newtheorem{definition}{Definition}
\theoremstyle{remark} 
\newtheorem{example}{Example}
\newtheorem{remark}{Remark}

%% definitions %%%%%%%%% 
\def\C{\mathbb C}
\def\R{\mathbb R}
\def\Q{\mathbb Q}
\def\Z{\mathbb Z}
\def\N{\mathbb N}

\def\p{\partial}
\DeclareMathOperator{\supp}{supp}
\newcommand{\pair}[1]{\left\langle #1 \right\rangle}
\newcommand{\norm}[1]{\left\|#1 \right\|}
%\newcommand{\dd}[1]{\mathrm{d}#1}
%\def\inter{\text{int}}
%\let\dim\relax
%\DeclareMathOperator{\dim}{dim}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\rhead{Model G Vortical Motion Group}
%\lhead{}
\rfoot{Page \thepage}


%\begin{document}
    
\begin{document}
    
\section{``In-text'' listing highlighting}
    
\begin{python}
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '-5 1 2 3 -4'
        -10
        print bla
\end{python}
    
    %\section{External listing highlighting}
    
    %\pythonexternal{demo.py}
    
    \section{Inline highlighting}
    
    Definition \pythoninline{class MyClass} means \dots
    
\end{document}

在此处输入图片描述

答案1

我认为这是字体问题。使用 时\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12},字体没有减号。我不确定你为什么不尝试类似

\newcommand\pythonstyle{\lstset{
        language=Python,
        basicstyle=\ttfamily,
        otherkeywords={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            % 
}}

以防万一您不知道,该minted包在 的帮助下提供了更好的语法高亮结果pygments

在此处输入图片描述

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{minted}

\usemintedstyle{manni}

\newmintinline[pyinl]{Python}{}

\begin{document}


\begin{minted}{python}
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '-5 1 2 3 -4'
        -10
        print bla
\end{minted}

\pyinl|class MyClass|

\end{document}

相关内容