无法在 LaTeX 输出中看到颜色 - lstlistings

无法在 LaTeX 输出中看到颜色 - lstlistings

你能帮我一下吗?下面我只看到黑色文本,尽管我用不同的颜色设置了很多东西...为什么这不起作用?

\documentclass[10pt,twoside,a4paper]{article}
%Packages
\usepackage[hmargin=2cm,vmargin=2cm,bmargin=2cm]{geometry}
\usepackage{listings}
\usepackage[portuguese]{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}% Include figure files
\usepackage{tabu}
\usepackage{dcolumn}% Align table columns on decimal point (with d instead of p{}, l, r, c, etc.)
\usepackage{placeins}
\usepackage{mathtools}
\renewcommand{\arraystretch}{1}
\renewcommand{\thetable}{\arabic{table}}
\usepackage{color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstset{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{blue},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

...

\begin{lstlisting}
void LUdecomposition3   (float*, float*, float*, int);
//receives as arguments 3 arrays of doubles with the diagonals'
//coefficients the values pointed to are changed within this
// method so that they can be used in the following method : LUsolve3


void LUsolve3       (float*, float*, float*, float*, float*, int);
//
\end{lstlisting}

抱歉给出了这么多可能无关紧要的东西,但我不知道错误在哪里......

提前致谢!!

答案1

问题是您没有指定要使用的语言,因此包无法识别哪些字符串作为关键字,哪些字符串作为注释等。只要您指定了正确的语言(预定义语言(参见包文档中的表 1)或用户定义的语言(另请参阅包文档)),颜色就会适当地应用。

我假设您的代码C++仅对应于以下示例:

\documentclass[10pt,twoside,a4paper]{article}
%Packages
\usepackage[hmargin=2cm,vmargin=2cm,bmargin=2cm]{geometry}
\usepackage{listings}
\usepackage[portuguese]{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}% Include figure files
\usepackage{tabu}
\usepackage{dcolumn}% Align table columns on decimal point (with d instead of p{}, l, r, c, etc.)
\usepackage{placeins}
\usepackage{mathtools}
\renewcommand{\arraystretch}{1}
\renewcommand{\thetable}{\arabic{table}}
\usepackage{color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstset{
    language=C++,
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{blue},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,
    columns=fullflexible,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\begin{document}

\begin{lstlisting}
void LUdecomposition3   (float*, float*, float*, int);
//receives as arguments 3 arrays of doubles with the diagonals'
//coefficients the values pointed to are changed within this
// method so that they can be used in the following method : LUsolve3


void LUsolve3       (float*, float*, float*, float*, float*, int);
//
\end{lstlisting}

\end{document}

结果:

在此处输入图片描述

一些建议:

  • 代码通常使用等宽字体排版,因此我将其添加\ttfamily到了密钥规范中basicstyle
  • 您当前的字体大小 ( \footnotesize) 似乎太小。或许可以考虑更改为\small

相关内容