首页 > 其他分享 >LaTeX 插入代码

LaTeX 插入代码

时间:2024-04-24 14:35:37浏览次数:20  
标签:LaTeX verbatim color 代码 listings 插入 rgb VT np

LaTeX 插入代码可以使用 verbatim 或者 fancyvrb 或者 listings 包。verbatim 没有语法高亮功能,只是显示一个等宽字体的输出。查看 Overleaf 示例

% Preamble
\usepackage{verbatim}
% Body
\begin{verbatim}
Text enclosed inside \texttt{verbatim} environment 
is printed directly
and all \LaTeX{} commands are ignored.
\end{verbatim}

而 listings 则带有语法高亮、显示行号等功能。

% Preamble
\usepackage{xcolor}  % 为了渲染颜色,需要使用 xcolor 包
\usepackage{listings}  % 渲染代码块
% Body

% 定义颜色
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

% 定义 listings 风格,可以定义多个
\lstdefinestyle{mystyle}{
    % backgroundcolor=\color{backcolour},  % 背景色
    commentstyle= \color{red!50!green!50!blue!50},  % 注释的颜色
    keywordstyle= \color{blue!70},  % 关键字/程序语言中的保留字颜色
    numberstyle=\tiny\color{codegray},  % 左侧行号显示的颜色
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,  % 对过长的代码自动换行
    captionpos=b,
    keepspaces=true,
    numbers=left,  % 在左侧显示行号
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,  % 不显示字符串中的空格
    showtabs=false,
    tabsize=2,
    frame=single  % [none | single | shadowbox] 显示边框
}

\lstset{style=mystyle}  % 使用 listings 风格

\begin{lstlisting}[language=Python]
    import numpy as np

    def incmatrix(genl1,genl2):
        m = len(genl1)
        n = len(genl2)
        M = None #to become the incidence matrix
        VT = np.zeros((n*m,1), int)  #dummy variable

        #compute the bitwise xor matrix
        M1 = bitxormatrix(genl1)
        M2 = np.triu(bitxormatrix(genl2),1) 

        for i in range(m-1):
            for j in range(i+1, m):
                [r,c] = np.where(M2 == M1[i,j])
                for k in range(len(r)):
                    VT[(i)*n + r[k]] = 1;
                    VT[(i)*n + c[k]] = 1;
                    VT[(j)*n + r[k]] = 1;
                    VT[(j)*n + c[k]] = 1;

                    if M is None:
                        M = np.copy(VT)
                    else:
                        M = np.concatenate((M, VT), 1)

                    VT = np.zeros((n*m,1), int)

        return M
\end{lstlisting}

参考:Code listing | Overleaf

标签:LaTeX,verbatim,color,代码,listings,插入,rgb,VT,np
From: https://www.cnblogs.com/Undefined443/p/18155226

相关文章

  • 代码源初级课 轮廓dp
    //705网格.cpp:此文件包含"main"函数。程序执行将在此处开始并结束。//#include<iostream>#include<algorithm>#include<cstring>usingnamespacestd;/*http://oj.daimayuan.top/course/5/problem/253有一个n×m的网格,现在我们想用1×2的矩形铺满它,要求......
  • 使用SSH从公网服务器简易使用内网任意机器服务,比如从外部下载代码
    如果有一个台外部的机器waibu-host,位于阿里云,腾讯云,亚马逊,azure云等等; 假设你想从这个waibu-host上访问公司任意服务,下载代码,访问http服务,等等,或者你机器上的服务,不需要额外工具,只要ssh反向就可以实现; 借个图,如下: 举个例子,如果你想从公司内部......
  • 【转载】git push到远程指定分支(git拉取指定分支代码)
     一、pull操作1、将远程指定分支拉取到本地指定分支上:gitpullorigin<远程分支名>:<本地分支名> (注:命令里的尖括号<>只是包裹中文的标识,方便你看的,实际使用时不用写,不过冒号需要)2、将远程指定分支拉取到本地当前分支上:gitpullorigin<远程分支名> 3、将与......
  • 代码段——C#判断时间是否在某个范围
    目录1.使用DateTime.Compare()2.通过时间相减计算时间间隔,可以指定精确度1.使用DateTime.Compare()注意:两个比较的时间,一定与相同的时间精度,比如都精确到分钟,或都精确到日注意:这里我判断指定时间是否在时间范围的闭区间里///<summary>///判断指定的时间......
  • 将C++代码文件路径、行号、函数名称等打包到#pragma message输出的方法
    #include<iostream>#define__GEN_STRING_IMPL(x)#x#define__GEN_STRING(x)__GEN_STRING_IMPL(x)#define__GEN_LOCATION_STRING()__FILE__"("__GEN_STRING(__LINE__)"):"classCTestObject{public:voidprint1(){......
  • 对于代码覆盖率,以下说法错误的是:( )
    选项:A、这可以帮助发现是否存在冗余代码B、可以帮助确定代码行是否被完全执行C、可以帮助发现状态机跳转路径是否覆盖D、可以帮助确定功能需求是否完全实现答案:D解析:覆盖率从大的方面分为两类,一类是功能覆盖率(functioncoverage),另一类是代码覆盖率(codecoverage)。其中功......
  • js-splice方法【插入、删除、替换】
    splice()语法:arrayObject.splice(index,howmany,item1,.....,itemX)参数说明:参数描述index必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。howmany必需。要删除的项目数量。如果设置为0,则不会删除项目。item1,…,itemX可选。向数组添......
  • dedebiz列表添加自增序号代码
    dedebiz列表添加序号代码如下://默认从1开始自增[field:globalname=autoindex/]但是有时候我们可能不需要从1开始,比如从2开始,那么就需要这样写://实现从2或者其他数字开始自增修改@me+1即可[field:globalname=autoindexrunphp="yes"]@me=@me+1;[/field:global]如果想让它......
  • dedebiz常用标签调用代码
    dedebiz常用标签调用代码:1、网站首页标题调用标签{dede:global.cfg_webname/}或{dede:globalname='cfg_webname'/}2、网站首页描述调用标签3、栏目标题调用标签{dede:field.title/}_{dede:global.cfg_webname/}或{dede:field.seotitle/}4、文章标题调用标签{dede:fieldn......
  • 数据结构:双向链表的创建·插入·删除
    数据结构:双向链表的创建·插入·删除/***@filename:数据结构:双向链表的创建·插入·删除*@brief:实现双向链表的创建·插入·删除*@author :[email protected]*@date :2024/04/23*@version:1.0*@note:none*CopyRight(c......