使用 Python 中的 lstlisting 下划线

使用 Python 中的 lstlisting 下划线

我正在使用 lstlistigs 在 beamer 中显示 python 代码,但是当我尝试写句子时:bbox_to_anchor:

bbox\_to\_anchor

不显示第一个下划线,只显示第二个下划线。

在我的列表样式中,我有:

\usepackage{courier}
\usepackage{listingsutf8}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{textcomp}
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{brown}{rgb}{0.59, 0.29, 0.0}
\definecolor{OliveGreen}{rgb}{0,0.25,0}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{0.98\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\renewcommand{\lstlistingname}{Código}
\definecolor{Code}{rgb}{0,0,0}
\definecolor{Keywords}{rgb}{255,0,0}
\definecolor{Strings}{rgb}{255,0,255}
\definecolor{Comments}{rgb}{0,0,255}
\definecolor{Numbers}{rgb}{255,128,0}
\makeatletter
\newif\iffirstchar\firstchartrue
\newif\ifstartedbyadigit
\newif\ifprecededbyequalsign
\newcommand\processletter
{%
 \ifnum\lst@mode=\lst@Pmode%
 \iffirstchar%
    \global\startedbyadigitfalse%
  \fi
  \global\firstcharfalse%
\fi
}

 \newcommand\processdigit
 {%
 \ifnum\lst@mode=\lst@Pmode%
  \iffirstchar%
    \global\startedbyadigittrue%
  \fi
  \global\firstcharfalse%
  \fi
}

\lst@AddToHook{OutputOther}%
{%
\lst@IfLastOtherOneOf{=}
{\global\precededbyequalsigntrue}
{}%
}

\lst@AddToHook{Output}%
{%
\ifprecededbyequalsign%
  \ifstartedbyadigit%
    \def\lst@thestyle{\color{orange}}%
  \fi
\fi
\global\firstchartrue%
\global\startedbyadigitfalse%
\global\precededbyequalsignfalse%
}

\lstset{ 
language=Python,
basicstyle=\footnotesize\ttfamily,
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=5pt,
backgroundcolor=\color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=t,
breakatwhitespace=false,
escapeinside={\#},
stringstyle =\color{OliveGreen},
keywordstyle = \color{blue},
commentstyle = \color{black},
identifierstyle = \color{black},
literate=%
     {á}{{\'a}}1
     {é}{{\'e}}1
     {í}{{\'i}}1
     {ó}{{\'o}}1
     {ú}{{\'u}}1
}

\lstdefinestyle{FormattedNumber}{%
literate={0}{{\textcolor{red}{0}}}{1}%
         {1}{{\textcolor{red}{1}}}{1}%
         {2}{{\textcolor{red}{2}}}{1}%
         {3}{{\textcolor{red}{3}}}{1}%
         {4}{{\textcolor{red}{4}}}{1}%
         {5}{{\textcolor{red}{5}}}{1}%
         {6}{{\textcolor{red}{6}}}{1}%
         {7}{{\textcolor{red}{7}}}{1}%
         {8}{{\textcolor{red}{8}}}{1}%
         {9}{{\textcolor{red}{9}}}{1}%
         {.0}{{\textcolor{red}{.0}}}{2}% Following is to ensure that only periods
         {.1}{{\textcolor{red}{.1}}}{2}% followed by a digit are changed.
         {.2}{{\textcolor{red}{.2}}}{2}%
         {.3}{{\textcolor{red}{.3}}}{2}%
         {.4}{{\textcolor{red}{.4}}}{2}%
         {.5}{{\textcolor{red}{.5}}}{2}%
         {.6}{{\textcolor{red}{.6}}}{2}%
         {.7}{{\textcolor{red}{.7}}}{2}%
         {.8}{{\textcolor{red}{.8}}}{2}%
         {.9}{{\textcolor{red}{.9}}}{2}%
         {\ }{{ }}{1}% handle the space
     ,%
      escapeinside={__}
      }

在我的 document.tex 文件中:

\documentclass[12pt]{beamer}
\input{../Preambulos/pre_codigo}
\begin{document}
\begin{frame}[plain, allowframebreaks, fragile]
\frametitle{Código para la solución}
\begin{lstlisting}[caption=Código para el método de disparo, style=FormattedNumber, basicstyle=\linespread{1.1}\ttfamily=\small, columns=fullflexible]
plt.plot(x,y_1_[:,_0_],'r+', label='solucion con alfa de prueba')
plt.plot(x,exacta(x),'b-', label='solucion exacta')
plt.xlabel('x')
plt.ylabel('u(x)')
plt.title('Metodo de disparo con $\alpha = 1.0$')
plt.xlim(0, 1)
plt.axhline(y = 0, color='k', lw=0.75, ls='dashed')
plt.legend(loc='lower left', bbox\_to\_anchor=(0.5, 0.35))
plt.show()
\end{lstlisting}
\end{frame}
\end{document}

然后,当我编译 latex 文件时,得到这个:

在此处输入图片描述

我希望所有的信息都能传达出去!

再次提前感谢!!

古斯塔沃。

答案1

出现此问题的原因是您使用了_转义字符。要避免此问题,您可以使用转义字符并通过 latex 代码将下划线输入为\textunderscore。或者您可以使用其他转义字符。

还请注意,您不需要xcolor使用color投影机。

\documentclass{beamer}

\usepackage{listingsutf8}
\usepackage{listings}
%\usepackage{xcolor}
%\usepackage{color}

\renewcommand{\lstlistingname}{Código}
\definecolor{Code}{rgb}{0,0,0}
\definecolor{Keywords}{rgb}{255,0,0}
\definecolor{Strings}{rgb}{255,0,255}
\definecolor{Comments}{rgb}{0,0,255}
\definecolor{Numbers}{rgb}{255,128,0}

\lstset{ 
language=Python,
basicstyle=\footnotesize\ttfamily,
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=5pt,
backgroundcolor=\color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=t,
breakatwhitespace=false,
escapeinside={\#},
stringstyle =\color{OliveGreen},
keywordstyle = \color{blue},
commentstyle = \color{black},
identifierstyle = \color{black},
literate=%
     {á}{{\'a}}1
     {é}{{\'e}}1
     {í}{{\'i}}1
     {ó}{{\'o}}1
     {ú}{{\'u}}1
}

\lstdefinestyle{FormattedNumber}{%
literate={0}{{\textcolor{red}{0}}}{1}%
         {1}{{\textcolor{red}{1}}}{1}%
         {2}{{\textcolor{red}{2}}}{1}%
         {3}{{\textcolor{red}{3}}}{1}%
         {4}{{\textcolor{red}{4}}}{1}%
         {5}{{\textcolor{red}{5}}}{1}%
         {6}{{\textcolor{red}{6}}}{1}%
         {7}{{\textcolor{red}{7}}}{1}%
         {8}{{\textcolor{red}{8}}}{1}%
         {9}{{\textcolor{red}{9}}}{1}%
         {.0}{{\textcolor{red}{.0}}}{2}% Following is to ensure that only periods
         {.1}{{\textcolor{red}{.1}}}{2}% followed by a digit are changed.
         {.2}{{\textcolor{red}{.2}}}{2}%
         {.3}{{\textcolor{red}{.3}}}{2}%
         {.4}{{\textcolor{red}{.4}}}{2}%
         {.5}{{\textcolor{red}{.5}}}{2}%
         {.6}{{\textcolor{red}{.6}}}{2}%
         {.7}{{\textcolor{red}{.7}}}{2}%
         {.8}{{\textcolor{red}{.8}}}{2}%
         {.9}{{\textcolor{red}{.9}}}{2}%
         {\ }{{ }}{1}% handle the space
     ,%
      escapeinside={__}
      }

\begin{document}

\begin{frame}[fragile]
\begin{lstlisting}[style=FormattedNumber]
bbox_\textunderscore_to_\textunderscore_anchor
\end{lstlisting}
\end{frame}

\end{document}

相关内容