![打印源代码的另一种方法](https://linux22.com/image/251786/%E6%89%93%E5%8D%B0%E6%BA%90%E4%BB%A3%E7%A0%81%E7%9A%84%E5%8F%A6%E4%B8%80%E7%A7%8D%E6%96%B9%E6%B3%95.png)
答案1
我建议使用listings
包。
例如
\documentclass[twocolumn,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[turkish]{babel}
\usepackage{listings}
\lstset{language=C++}
\lstset{breaklines=true}
\begin{document}
\newpage
\onecolumn
\begin{lstlisting}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
typedef struct word //struct to contain words
{
char *string;
} word;
void print(int ** matrix, char str [], int n)
//this is a comment too long to fit on a line. So listings will break this line because I used \lstset{breaklines=true} But this is not default
{
printf("--- %s ---\n",str);
int i,j;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
if(matrix[i][j]!=INT_MAX)
printf("%3d ",matrix[i][j]);
else(printf("%3s ","INF"));
printf("\n");
}
}
\end{lstlisting}
\end{document}
使用 LaTeX 生成此输出
如您所见,\lstset{language=C++}
在代码中定义语言。我更喜欢这种方法,因为我认为它更容易阅读。