代码之家  ›  专栏  ›  技术社区  ›  sixtyfootersdude

在此文档中插入带有缩进的代码

  •  351
  • sixtyfootersdude  · 技术社区  · 15 年前

    \begin{code}## Heading ##
    ...
    \end{code}
    

    我唯一真正需要的是缩进和固定宽度的字体。语法高亮显示可能很好,尽管它绝对不是必需的。

    7 回复  |  直到 6 年前
        1
  •  684
  •   Cloudanger    6 年前

    使用 listings

    乳胶头的简单配置(之前 \begin{document}

    \usepackage{listings}
    \usepackage{color}
    
    \definecolor{dkgreen}{rgb}{0,0.6,0}
    \definecolor{gray}{rgb}{0.5,0.5,0.5}
    \definecolor{mauve}{rgb}{0.58,0,0.82}
    
    \lstset{frame=tb,
      language=Java,
      aboveskip=3mm,
      belowskip=3mm,
      showstringspaces=false,
      columns=flexible,
      basicstyle={\small\ttfamily},
      numbers=none,
      numberstyle=\tiny\color{gray},
      keywordstyle=\color{blue},
      commentstyle=\color{dkgreen},
      stringstyle=\color{mauve},
      breaklines=true,
      breakatwhitespace=true,
      tabsize=3
    }
    

    您可以使用更改文档中间的默认语言 \lstset{language=Java} .

    文档中的用法示例:

    \begin{lstlisting}
    // Hello.java
    import javax.swing.JApplet;
    import java.awt.Graphics;
    
    public class Hello extends JApplet {
        public void paintComponent(Graphics g) {
            g.drawString("Hello, world!", 65, 95);
        }    
    }
    \end{lstlisting}
    

    结果如下:

    Example image

        2
  •  186
  •   midtiby    15 年前

    您还可以使用逐字记录环境

    \begin{verbatim}
    your
    code
    example
    \end{verbatim}
    
        3
  •  121
  •   alan    5 年前

    下面是如何添加内联代码:

    {\tt code } \texttt{ code }

    \newcommand{\code}[1]{\texttt{#1}}
    

    另外,请注意,代码块可以从具有

    \lstinputlisting[breaklines]{source.c}
    

    breaklines 不需要,但我觉得很有用。请注意,您必须指定 \usepackage{ listings } 为了这个。

    更新: listings包还包括 \lstinline 命令,该命令与 \lstlisting \lstinputlisting \mintinline 命令。就像 \LST内联 \薄荷糖

    \documentclass{article}
    
    \usepackage{minted}
    
    \begin{document}
      This is a sentence with \mintinline{python}{def inlineCode(a="ipsum)}
    \end{document}
    
        4
  •  33
  •   Philipp    15 年前

    专门的软件包,如 minted ,它依赖于Pygments来进行格式化,与 listings 包裹。引用 铸币

    与传统软件包相比,Pygments提供了更优越的语法高亮显示。例如,清单基本上只突出显示字符串、注释和关键字。另一方面,Pygments可以完全定制为突出显示源语言可能支持的任何令牌类型。这可能包括字符串、数字、不同类型的标识符和外来结构(如HTML标记)中的特殊格式序列。

        5
  •  13
  •   XavierStuvw Shital Shah    5 年前

    铸币 ,是否来自 GitHub CTAN, the Comprehensive TeX Archive Network ,就职于 Overleaf ,TeX Live和MiKTeX。

    Pygments ; 以上任一来源的文档对此进行了解释。尽管Pygments将自己标榜为Python语法的亮点,但Minted保证了对数百种其他语言的覆盖。

    \documentclass{article}
    \usepackage{minted}
    \begin{document}
    
    \begin{minted}[mathescape, linenos]{python}
    
    # Note: $\pi=\lim_{n\to\infty}\frac{P_n}{d}$
    title = "Hello World"
    
    sum = 0
    for i in range(10):
     sum += i
    
    \end{minted}
    
    \end{document}
    

    输出:

    enter image description here

        6
  •  12
  •   coffeemakr CaptainCrunch    7 年前

    使用 Minted

    它是一个使用强大的 Pygments fancyvrb .

    它比任何其他软件包都更加进化和可定制!

        7
  •  6
  •   MattAllegro    6 年前

    spverbatim (无语法突出显示):

    \documentclass{article}
    \usepackage{spverbatim}
    
    \begin{document}
    
    \begin{spverbatim}
      Your code here
    \end{spverbatim}
    
    \end{document}
    

    alltt :

    \documentclass{article}
    \usepackage{alltt}
    
    \begin{document}
    
    \begin{alltt}
      Your code here
    \end{alltt}
    
    \end{document}
    
        8
  •  5
  •   Tarantula    15 年前

    使用 Pygments !

        9
  •  3
  •   Morey    5 年前

    \documentclass[11pt]{article}  
    \usepackage{pythonhighlight}
    
    \begin{document}
    
    The following is some Python code
    
    \begin{python}
    # A comment
    x = [5, 7, 10]
    y = 0
    
    for num in x:
        y += num
        
    print(y)
    \end{python}
    
    \end{document}
    

    看起来像: enter image description here

    不幸的是,这只适用于Python。