美文网首页效率
在LaTeX中插入程序代码

在LaTeX中插入程序代码

作者: 野狗子嗷嗷嗷 | 来源:发表于2016-12-12 22:31 被阅读3956次

一.代码高亮

解决方案一:minted宏包

\documentclass[a4paper]{ctexart}
\usepackage{minted}

\begin{document}
示例1
\begin{minted}{c++}
int main() {
    printf("hello, world");
    return 0;
}
\end{minted}

示例2
\begin{minted}[mathescape,
               linenos,
               numbersep=5pt,
               gobble=2,
               frame=lines,
               framesep=2mm]{csharp}
  string title = "This is a Unicode π in the sky"
  /*
  Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeter
  of an $n$-sided regular polygon circumscribing a
  circle of diameter $d$.
  */
  const double pi = 3.1415926535
\end{minted}
\end{document}

解决方案二:listings宏包

\documentclass[a4paper]{ctexart}
\usepackage{listings} 
\usepackage{xcolor} 
\usepackage{fontspec}
\newfontfamily\monaco{Monaco}
\setmonofont[Mapping={}]{Monaco} %英文引号之类的正常显示,相当于设置英文字体\setsansfont{Monaco} %设置英文字体 Monaco, Consolas, Fantasque Sans Mono
\setmainfont{Monaco} %设置英文字体
\setCJKmainfont{方正兰亭黑简体} %中文字体设置
%\setCJKsansfont{华康少女字体} %设置中文字体
%\setCJKmonofont{华康少女字体} %设置中文字体


% 全局设置
\lstset{columns=flexible,numbers=left,numberstyle=\tiny\monaco,basicstyle=\small\monaco,keywordstyle= \color{ blue!70},commentstyle=\color{red!50!green!50!blue!50}, frame=shadowbox, rulesepcolor= \color{ red!20!green!20!blue!20} } 

\begin{document}
% 默认
\begin{ lstlisting}[ language=C] 
int main(int argc, char ** argv) 
{ 
    printf("Hello world! \n"); 
    return 0; 
} 
\end{ lstlisting} 

% 添加边框
\begin{lstlisting}[language={[ANSI]C},
                        keywordstyle=\color{blue!70},
                        commentstyle=\color{red!50!green!50!blue!50},
                        frame=shadowbox, 
                                                numbers=left, 
                        rulesepcolor=\color{red!20!green!20!blue!20}] 
{ 
    printf("Hello world! \n"); 
    return 0; 
} 
\end{lstlisting}


\end{ document}

自定义配置

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\lstset{ %
backgroundcolor=\color{white},   % choose the background color
basicstyle=\footnotesize\ttfamily,        % size of fonts used for the code
columns=fullflexible,
breaklines=true,                 % automatic line breaking only at whitespace
captionpos=b,                    % sets the caption-position to bottom
tabsize=4,
commentstyle=\color{mygreen},    % comment style
escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
keywordstyle=\color{blue},       % keyword style
stringstyle=\color{mymauve}\ttfamily,     % string literal style
frame=single,
rulesepcolor=\color{red!20!green!20!blue!20},
% identifierstyle=\color{red},
language=c++,
}
\lstset{ %
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\ttfamily,            % 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=b,                    % sets the caption-position to bottom
  commentstyle=\ttfamily\color{mygreen},    
                                   % comment style
  deletekeywords={},               % if you want to delete keywords from the given language
  escapeinside={},                 % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,                    % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=C++,                    % the language of the code
  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{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  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{mymauve},     % string literal style
  tabsize=2,                       % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

二.伪代码

样式一

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\begin{document}
\begin{algorithm}
  \caption{Calculate $y = x^n$}         %单独一栏标题
  \label{alg1}                          %标签显示为:Algorithm 1
  \begin{algorithmic}                   %开始algorithmic环境
  \REQUIRE $n \geq 0 \vee x \neq 0$     %\REQUIRE显示Require:
  \ENSURE $y = x^n$                     %\ENSURE显示Ensure:
  
  \STATE $y \gets 1$                    %\gets和\leftarrow一样都是左箭头,建议用\gets
  \IF{$n < 0$}                          %\IF{$xxx$}显示if xxx then
      \STATE $X \gets 1 / x$                %每一行都要\STATE $xxx$
      \STATE $N \gets -n$
  \ELSE                                 %\ELSE显示else
      \STATE $X \gets x$
      \STATE $N \gets n$
  \ENDIF                                %\ENDIF显示end if
  \WHILE{$N \neq 0$}                    %\WHILE{$xxx$}显示while xxx do
      \IF{$N$ is even}
          \STATE $X \gets X \times X$
          \STATE $N \gets N / 2$
      \ELSE[$N$ is odd]                 %ELSE[$xxx$]显示else {xxx}
          \STATE $y \gets y \times X$
          \STATE $N \gets N - 1$
      \ENDIF
  \ENDWHILE                             %%\ENDWHILE显示end while
  \end{algorithmic}
\end{algorithm}
\end{document} 

知乎-在 LaTeX 中排版算法(伪代码),如何正确缩进?

样式二

\documentclass{article}
\usepackage{xeCJK}
\usepackage[ruled]{algorithm2e}

\renewcommand{\algorithmcfname}{算法} 

\begin{document}
\begin{algorithm}[H]
% \SetAlgoNoLine可以去掉竖线
\caption{How to write algorithms}
\KwIn{this text}
\KwOut{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
    read current\;
    \eIf{understand}{
        go to next section\;
        current section becomes this one\;
    }{
        go back to the beginning of current section\;
    }
}
\end{algorithm}
\end{document}  

样式三

\documentclass{article}
\usepackage{xeCJK}
\usepackage[linesnumbered,boxed]{algorithm2e}

\begin{document}
\begin{algorithm}
\caption{identifyRowContext}
\KwIn{$r_i$ , $Backgrd(T_i)$=${T_1,T_2,\ldots,T_n}$ and similarity threshold $\theta_r$}
\KwOut{$con(r_i)$}
$con(r_i)= \Phi$\;
\For{$j=1;j \le n;j \ne i$}
{
 float $maxSim=0$\;
 $r^{maxSim}=null$\;
  \While{not end of $T_j$}
 {
     compute Jaro($r_i,r_m$)($r_m \in T_j$)\;
     \If{$(Jaro(r_i,r_m) \ge \theta _r) \wedge ((Jaro(r_i,r_m) \ge r^{maxSim}) $}
      {
           replace $r^maxSim$ with $r_m$\;
      }
 }
$con(r_i)=con(r_i) \cup {r^{maxSim}}$\;
}
return $con(r_i)$\;
\end{algorithm} 
\end{document}  

相关文章

  • 在LaTeX中插入程序代码

    一.代码高亮 解决方案一:minted宏包 解决方案二:listings宏包 自定义配置 二.伪代码 样式一 知乎...

  • LaTeX笔记--(4)--[嵌入图像]

    有时,我们会需要在文档中插入图像。在LaTeX中,使用figure环境和graphicx包插入图片,图片将会被自动...

  • 在Latex文档中插入算法模块

    1.前言 作为一名计算机专业的学生,在写学术论文的时候,不可避免的需要在论文中书写相关的算法。如果自己一点点调格式...

  • LaTeX:在pdf中插入动图

    前言 用LaTeX直接生成的pdf文件,理论上是不能直接插入.gif文件的!但是有些原理图(GeoGebra作原理...

  • 在word中插入latex伪代码

    使用word时,需要插入latex样式的伪代码,如下所示: 安装后打开word,如果有插件直接在插件里打开,没有的...

  • 2018-04-03

    【LaTeX】Lyx/LaTeX笔记01---插入伪代码[latex]使用algorithm包来编写伪代码 - C...

  • LaTeX Beamer中插入视频

    Embedding Videos in LaTeX Beamer Introduction to insertin...

  • 在Word中优雅地插入Latex公式

    1.准备 神器:1. Mathpix Snippinghttps://mathpix.com/[https://m...

  • 如何在 PPT 中插入 latex?

    2020.06.26 Friday @SG 同事问如何在 ppt 中插入 latex 公式。这个问题比较常遇到,写...

  • Latex公式编辑

    前言 相信很多人都有这样痛苦的体验, 给文档中插入公式. 后来无意间发现 latex对于文档中插入公式特别方便,一...

网友评论

本文标题:在LaTeX中插入程序代码

本文链接:https://www.haomeiwen.com/subject/dzlvmttx.html