如何将列表代码与文本左对齐

如何将列表代码与文本左对齐

我使用listings包将示例代码包含到我的文档中,并frame使用选项将代码放在一个框内。问题是,框架左侧规则和代码之间的空白太多了(见下图)。

问题似乎不是框架本身,而是代码的左对齐。我尝试更新选项xleftmargin,但什么也没发生。我可以减少空白的唯一方法是用选项给框架的左规则一个负长度framexleftmargin,但我不喜欢它,因为我希望框架宽度与文本宽度相同。我只想减少左侧空白以利用整行并减少代码中的断线。

这是我当前的配置。

\lstset{language=Java,
    keywordstyle=\color{RoyalBlue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black}
} 

请帮我解决这个问题。我被这个问题困扰了好几个小时。

谢谢。

编辑:根据需要提供了一个最小的工作示例。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
    keywordstyle=\color{RoyalBlue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black}
}

\begin{document}
    \begin{lstlisting}
        public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
            LongWritable mkey = new LongWritable();
            LongWritable mval = new LongWritable();

            public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
                String line = value.toString();
                StringTokenizer tokenizer = new StringTokenizer(line);
                long src, dst;

                if (tokenizer.hasMoreTokens()) {
                    src = Long.parseLong(tokenizer.nextToken());
                    if (!tokenizer.hasMoreTokens())
                        throw new RuntimeException("Invalid edge line: " + line);
                    dst = Long.parseLong(tokenizer.nextToken());

                    mkey.set(src);
                    mval.set(dst);
                    context.write(mkey, mval);
                    context.write(mval, mkey);
                }
            }
        }
    \end{lstlisting}
\end{document}

列表和框架存在问题

答案1

只需加载lstautogobble打包并插入autogobble=true到您的中\lstset,这样您就不必担心代码缩进。

平均能量损失

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings,lstautogobble}
\usepackage{xcolor}

\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
    keywordstyle=\color{RoyalBlue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black},
    autogobble=true
}

\begin{document}
    \begin{lstlisting}
        public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
            LongWritable mkey = new LongWritable();
            LongWritable mval = new LongWritable();

            public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
                String line = value.toString();
                StringTokenizer tokenizer = new StringTokenizer(line);
                long src, dst;

                if (tokenizer.hasMoreTokens()) {
                    src = Long.parseLong(tokenizer.nextToken());
                    if (!tokenizer.hasMoreTokens())
                        throw new RuntimeException("Invalid edge line: " + line);
                    dst = Long.parseLong(tokenizer.nextToken());

                    mkey.set(src);
                    mval.set(dst);
                    context.write(mkey, mval);
                    context.write(mval, mkey);
                }
            }
        }
    \end{lstlisting}
\end{document} 

输出:

在此处输入图片描述

autogobble选项会根据第一行的空格数吞噬整个列表中的空格。如果已经实现了这样的包,那么意味着你的问题一点也不愚蠢!

答案2

只需删除代码前面的空格:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
    keywordstyle=\color{RoyalBlue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black}
}

\begin{document}
    \begin{lstlisting}
public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
    LongWritable mkey = new LongWritable();
    LongWritable mval = new LongWritable();

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        long src, dst;

        if (tokenizer.hasMoreTokens()) {
            src = Long.parseLong(tokenizer.nextToken());
            if (!tokenizer.hasMoreTokens())
                throw new RuntimeException("Invalid edge line: " + line);
            dst = Long.parseLong(tokenizer.nextToken());

            mkey.set(src);
            mval.set(dst);
            context.write(mkey, mval);
            context.write(mval, mkey);
        }
    }
}
    \end{lstlisting}
\end{document}

答案3

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}
\parindent=0pt
\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
    keywordstyle=\color{RoyalBlue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black},
    xleftmargin=\dimexpr\fboxsep-\fboxrule,
    xrightmargin=\dimexpr\fboxsep-\fboxrule,
    gobble=8
}

\begin{document}

foo bar bza \hfill foo bar bza

\begin{lstlisting}
        public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
            LongWritable mkey = new LongWritable();
            LongWritable mval = new LongWritable();

            public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
                String line = value.toString();
                StringTokenizer tokenizer = new StringTokenizer(line);
                long src, dst;

                if (tokenizer.hasMoreTokens()) {
                    src = Long.parseLong(tokenizer.nextToken());
                    if (!tokenizer.hasMoreTokens())
                        throw new RuntimeException("Invalid edge line: " + line);
                    dst = Long.parseLong(tokenizer.nextToken());

                    mkey.set(src);
                    mval.set(dst);
                    context.write(mkey, mval);
                    context.write(mval, mkey);
                }
            }
        }
\end{lstlisting}
\end{document}

在此处输入图片描述

相关内容