lstinputlistings 的 autogobble

lstinputlistings 的 autogobble

在 listings 手册中,它说gobble对 没有任何影响lstinputlistings。由于该手册已经有四年的历史了,在阅读之后 如何自动跳过列表中的前导空格我的问题是是否有办法激活gobble甚至更好的autogobble方法lstinputlistings

编辑:我已尝试过\lstinputlistings[gobble=3]{hello.c},但没有作用,并且我已经设置了tabsize=3

梅威瑟:

\documentclass{article}

\usepackage{listings}
\lstset{tabsize=3,frame=single,numbers=left}

\usepackage{filecontents}
\begin{filecontents*}{hello.c}
if (a<b){
    if (b<a){
        //do something
    }
}
\end{filecontents*}

\begin{document}
\noindent
Using \verb|\begin{lstlisting}... gobble=3| works:
\begin{lstlisting}[gobble=3,firstnumber=2,linerange={2-4}]
if (a<b){
    if (b<a){
        //do something
    }
}
\end{lstlisting}

\bigskip\noindent
Using \verb|\lstinputlisting gobble| does not work:
\lstinputlisting[gobble=3,firstnumber=2,linerange={2-4}]{hello.c}
\end{document}

在此处输入图片描述

更新:在研究了 S. Murugan 的代码之后,我想到了这个,它还根据 firstline 设置了 firstnumber——这也是我想要的。

\documentclass{article}
\usepackage{listings,xifthen,filecontents}

\lstset{tabsize=3,numbers=left,frame=single,basicstyle=\Huge\ttfamily,columns=flexible}
\newlength{\gobble}
\newlength{\gobblea}
% The width of a single space. basicstyle from lstset should be used
\sbox0{\Huge\ttfamily \ }
\newcommand{\mylist}[5]{
%#1 is number of tabs, could be calculated like in listings-autogobble with autogobble=true or be an extra option
%#2 is tabsize, which is set in lstset
%#3 is firstline from lstset
%#4 is lastline from lstset
%#5 is the filename, the only thing which should be an argument and not an option.

% Remove a single space
\setlength{\gobble}{-\the\wd0}
% Reindent a bit by dividing by 1.1, then multiply by tabsize and number of indentation levels
\setlength{\gobble}{0.90909090909090909090909090909091\gobble*#1*#2}
\setlength{\gobblea}{\gobble}
\addtolength{\gobblea}{10pt}
% Check if firstline is defined
\ifthenelse{\isempty{#3} \OR \equal{#3}{0}}{%
% Check if lastline is defined
\ifthenelse{\isempty{#4}}{%
\lstinputlisting[firstnumber=1,firstline=1,framexleftmargin=\gobble,xleftmargin=\gobble,numbersep=\gobblea]{#5}
}{
\lstinputlisting[firstnumber=1,firstline=1,lastline=#4,framexleftmargin=\gobble,xleftmargin=\gobble,numbersep=\gobblea]{#5}
}
}{
\ifthenelse{\isempty{#4}}{%
\lstinputlisting[firstnumber=#3,firstline=#3,framexleftmargin=\gobble,xleftmargin=\gobble,numbersep=\gobblea]{#5}
}{
\lstinputlisting[firstnumber=#3,firstline=#3,lastline=#4,framexleftmargin=\gobble,xleftmargin=\gobble,numbersep=\gobblea]{#5}
}
}
}

\begin{filecontents*}{hello.c}
if (b<a){
   //do something
}
\end{filecontents*}
\begin{filecontents*}{hello1.c}
if (a<b){
   if (b<a){
      //do something
   }
}
\end{filecontents*}

\begin{document}
\lstinputlisting[firstnumber=2,linerange={1-3}]{hello.c}
%mylist{#tabs}{#tabsize}{firstline}{lastline}{filename}
\mylist{1}{3}{2}{4}{hello1.c}
\mylist{2}{3}{3}{3}{hello1.c}
\end{document}

更新\lstinputlisting[tabs=1,firstline=2,lastline=4]{hello.c}现在它应该适用于任何字体大小(测试范围从 8 到 60pt)和任何基本样式(测试范围从 \tiny 到 \Huge)。如果我可以只写,它就会从 中获取,tabsize=3那就basicstyle=\Huge\ttfamily太好了lstset{}

2013 年 7 月 6 日更新如何扩展 \lstinputlisting 命令有更好的方法来实现期望的结果。

答案1

现在检查一下:

\documentclass{article}

\usepackage{listings}
\lstset{tabsize=3,frame=single,numbers=left}

\usepackage{filecontents}
\begin{filecontents*}{hello.c}
if (a<b){
    if (b<a){
        //do something
    }
}
\end{filecontents*}

\begin{document}
\noindent Using \verb|\begin{lstlisting}... gobble=3| works:
\begin{lstlisting}[gobble=3,firstnumber=2,linerange={2-4}]
if (a<b){
    if (b<a){
        //do something
    }
}
\end{lstlisting}

\bigskip\noindent
Using \verb|\lstinputlisting gobble| does not work:

\lstinputlisting[tabsize=2,showspaces=false,gobble=3,firstnumber=2,linerange={2-4},framexleftmargin=-16pt,numbersep=-8pt,xleftmargin=-18pt]{hello.c}

\end{document}

答案2

由于在搜索有关自动将 gobble 添加到列表时首先出现这个问题,因此这里有一个解决方案,只需根据第一行删除缩进,这要归功于\usepackage{lstautogobble}

\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{lstautogobble} % <== this line is important

\lstset{ %
  language=C,
  numbers=left,
  autogobble=true, % <== this line is important
}

\begin{document}

\begin{lstlisting}
  int main(){
    printf("hello");
    return 0;
  }
\end{lstlisting}

\end{document}

相关内容