latex插入代码块居左
时间: 2023-07-03 07:18:27 浏览: 603
你可以使用`lstlisting`宏包来插入代码块,并设置其对齐方式。具体来说,可以使用`lstset`命令来设置代码块的样式和对齐方式,如下所示:
```latex
\documentclass{article}
\usepackage{listings}
\lstset{
language=C,
basicstyle=\ttfamily,
keywordstyle=\color{blue},
commentstyle=\color{green},
numbers=left,
numberstyle=\tiny\color{gray},
frame=single,
breaklines=true,
breakatwhitespace=true,
tabsize=4,
% 设置对齐方式
xleftmargin=2em,
framexleftmargin=2em
}
\begin{document}
\begin{lstlisting}
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
\end{lstlisting}
\end{document}
```
其中,`xleftmargin`表示代码块左侧空白的宽度,`framexleftmargin`表示代码块边框左侧空白的宽度。将它们设置为相同的值,即可使代码块左对齐。
阅读全文
相关推荐














