latex-defined coding style

code.sty packages

The packages define Latex insert style code, including the packages in the preamble

\usepackage{code}

The following codes packages

% 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
}

The packages using Monaco font, Linux and Windows to be installed before using this font.

Latex code is inserted in two ways

  • Enter the code directly in the tex file
\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}
  • The code read from the source file
\documentclass{article}
\usepackage{ctex}
\usepackage{code}
\begin{document}
\lstinputlisting[language=C++, title=test.cpp]{test.cpp} % 从test.cpp源文件中读取代码
\end{document}

Typeset

Here Insert Picture Description

Published 42 original articles · won praise 5 · Views 2947

Guess you like

Origin blog.csdn.net/Function_RY/article/details/103677083
Recommended