1

I'm using lstlistigs to show python code in beamer, but when I try to write the sentence: bbox_to_anchor:

bbox\_to\_anchor

don't show the first underscore, only the second.

In my lstlisting style I have:

\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={__}
      }

In my document.tex file:

\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}

Then, when I compile the latex file, get this:

enter image description here

I hope all the information goes!

Thanks in adavance again!!

Gustavo.

  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user36296 Apr 4 '18 at 17:15
1

The problem occurs because you use _ as escape character. To circumvent this problem, you could use your escape characters and input the underscores via latex code as \textunderscore. Or you could use some other escape character.

Please also note that you don't need xcolor and color with beamer.

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.