如何在列表包中左对齐代码?

如何在列表包中左对齐代码?

我正在用 Beamer 制作一些关于 R 语言的幻灯片。我使用 listing 包来显示一些代码。问题是,如果我使用 autogobble 选项删除不必要的空间,源代码就会超出行号。请参见下面的图片。 带有不必要的空格和行号的代码

应用 autogobble = true 后我得到: 在此处输入图片描述

如何避免此问题?MWE 是:

\documentclass[aspectratio=169,xcolor = svgnames, handout]{beamer}

%Paquetes basicos
\usepackage[utf8]{inputenc}
\usepackage{ragged2e}

%Paquetes de estilo de la presentacion
\usetheme{CambridgeUS}
%\useoutertheme{varphi}

%Colores personalizados
\usepackage{color}
\definecolor{gray97}{gray}{.97}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{dkgreen}{rgb}{0,0.6,0}

%Paquetes para lenguajes de programacion
\usepackage{listings,lstautogobble}
\lstset{
    language = R,
    frame = tb,
    framerule = 0pt,
    aboveskip = 3mm,
    belowskip = 3mm,
    framextopmargin = 3pt,
    framexbottommargin = 3pt,
    framexleftmargin = 0.05cm,
    framexrightmargin = 0.05cm,
    framesep = 0pt,
    rulesep = .4pt,
    backgroundcolor = \color{gray97},
    rulesepcolor = \color{black},
    stringstyle = \color{mauve},
    showstringspaces = false,
    basicstyle = \small\ttfamily,
    commentstyle = \color{dkgreen},
    keywordstyle = \color{blue},
    numbers = left,
    numbersep = -6.5pt,
    numberstyle = \tiny\color{gray},
    numberfirstline = false,
    breaklines = true,
    autogobble=true,
    morekeywords = {*,...}
}

\begin{document}

\begin{frame}[fragile]{\textbf{Directorio de Trabajo}}
\begin{itemize}
    \item Para fijar el directorio de trabajo:
        \begin{lstlisting}
            setwd("~/Curso R/") 
        \end{lstlisting}
    \item Para obtener el directorio de trabajo:
        \begin{lstlisting}
            getwd()
        \end{lstlisting}
\end{itemize}
\end{frame}

\end{document}

答案1

您不必使用 将数字移动到文本区域中numbersep = -6.5pt,而是可以更改以xleftmargin使数字与周围文本对齐:

\documentclass[aspectratio=169,xcolor = svgnames, handout]{beamer}

%Paquetes basicos
\usepackage[utf8]{inputenc}
\usepackage{ragged2e}

%Paquetes de estilo de la presentacion
\usetheme{CambridgeUS}
%\useoutertheme{varphi}

%Colores personalizados
\usepackage{color}
\definecolor{gray97}{gray}{.97}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{dkgreen}{rgb}{0,0.6,0}

%Paquetes para lenguajes de programacion
\usepackage{listings,lstautogobble}
\lstset{
    language = R,
    frame = tb,
    framerule = 0pt,
    aboveskip = 3mm,
    belowskip = 3mm,
    framextopmargin = 3pt,
    framexbottommargin = 3pt,
    framexleftmargin = 0.7cm,
    xleftmargin=0.7cm,
    framexrightmargin = 0.05cm,
    framesep = 0pt,
    rulesep = .4pt,
    backgroundcolor = \color{gray97},
    rulesepcolor = \color{black},
    stringstyle = \color{mauve},
    showstringspaces = false,
    basicstyle = \small\ttfamily,
    commentstyle = \color{dkgreen},
    keywordstyle = \color{blue},
    numbers = left,
%    numbersep = -6.5pt,
    numberstyle = \tiny\color{gray},
    numberfirstline = false,
    breaklines = true,
    autogobble=true,
    morekeywords = {*,...}
}

\begin{document}

\begin{frame}[fragile]{\textbf{Directorio de Trabajo}}
\begin{itemize}
    \item Para fijar el directorio de trabajo:
        \begin{lstlisting}
setwd("~/Curso R/") 
        \end{lstlisting}
    \item Para obtener el directorio de trabajo:
        \begin{lstlisting}
            getwd()
        \end{lstlisting}
\end{itemize}
\end{frame}

\end{document}

在此处输入图片描述

相关内容