latex代码风格定义

本文介绍了如何使用code.sty宏包在LaTeX中定义代码的风格,包括宏包的包含方法、使用的Monaco字体以及在Linux和Windows系统中预装字体的要求。还提及了在LaTeX中插入代码的两种方式,即直接输入和从源文件读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

code.sty宏包

该宏包定义了Latex插入代码的风格,在导言区包含该宏包

\usepackage{code}

该宏包的代码如下

% Code style package
% writen by Liangjin Song on 20191224
\ProvidesPackage{code}

% add package
\usepackage{fontspec}
\usepackage{color,xcolor}
\usepackage{listings}

% define color
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codemauve}{rgb}{0.58,0,0.82}
\definecolor{codemauve}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

% code style
\lstset{%
  backgroundcolor=\color{backcolour},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\scriptsize\fontspec{Monaco},        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=t,                    % sets the caption-position to bottom
  columns=fullflexible,
  commentstyle=\color{codegreen}\fontspec{Monaco},    % comment style
  extendedchars=true,                                 % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  keepspaces=true,                                    % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue}\fontspec{Monaco},         % keyword style
  % morekeywords={*,...},                             % if you want to add more keywords to the set
  numbers=left,                                       % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                                      % how far the line-numbers are from the code
  numberstyle=\tiny\color{codegray},                  % the style that is used for the line-numbers
  showspaces=false,                                   % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,                             % underline spaces within strings only
  showtabs=false,                                     % show tabs within strings adding particular underscores
  stepnumber=1,                                       % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{codemauve}\fontspec{Monaco},     % string literal style
  tabsize=4                                           % sets default tabsize to 4 spaces
}

该宏包使用的是Monaco字体,Linux和Windows在使用前需安装这个字体。

Latex中插入代码有两种方式

  • 直接在tex文件中输入代码
\documentclass{article}
\usepackage{ctex}
\usepackage{code}
\begin{document}
\begin{lstlisting}[language=C++]
#include <iostream>
using namespace std;
int main(){
    cout << "Hello world!" << endl;
}
\end{lstlisting}
\end{document}
  • 从源文件中读取代码
\documentclass{article}
\usepackage{ctex}
\usepackage{code}
\begin{document}
\lstinputlisting[language=C++, title=test.cpp]{test.cpp} % 从test.cpp源文件中读取代码
\end{document}

排版效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值