LaTeX package for syntax highlighting of code in various languages

后端 未结 7 1936
温柔的废话
温柔的废话 2020-11-29 15:00

I am looking for a LaTeX package that does syntax highlighting on code. For example, right now I use the verbatim block to write code:

\\begin{verbatim}
             


        
7条回答
  •  情深已故
    2020-11-29 15:09

    I would use the minted package as mentioned from the developer Konrad Rudolph instead of the listing package. Here is why:

    listing package

    The listing package does not support colors by default. To use colors you would need to include the color package and define color-rules by yourself with the \lstset command as explained for matlab code here.

    Also, the listing package doesn't work well with unicode, but you can fix those problems as explained here and here.

    The following code

    \documentclass{article}
    \usepackage{listings}
    
    \begin{document}
    \begin{lstlisting}[language=html]
    
        
            Hello
        
        Hello
    
    \end{lstlisting}
    \end{document}
    

    produces the following image:

    minted package

    The minted package supports colors, unicode and looks awesome. However, in order to use it, you need to have python 2.6 and pygments. In Ubuntu, you can check your python version in the terminal with

    python --version
    

    and you can install pygments with

    sudo apt-get install python-pygments
    

    Then, since minted makes calls to pygments, you need to compile it with -shell-escape like this

    pdflatex -shell-escape yourfile.tex
    

    If you use a latex editor like TexMaker or something, I would recommend to add a user-command, so that you can still compile it in the editor.

    The following code

    \documentclass{article}
    \usepackage{minted}
    \begin{document}
    
    \begin{minted}{html}
        
        
           
               Hello
           
    
           Hello
        
    \end{minted}
    \end{document}
    

    produces the following image:

提交回复
热议问题