210206-结合tcolorbox与minted编写LaTex代码块

该博客介绍了如何在LaTeX文档中结合tcolorbox和minted包,创建带有注释、行号和高亮显示的代码块。示例展示了commentBox、codeInput和codeFromFile三种类型的代码展示,并提供了交叉引用功能。此外,还提到了如何设置mintedoptions以定制代码显示效果。

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

  • 效果图
    在这里插入图片描述
  • 源代码
\documentclass{article}

\newcounter{commentCount}
\newcounter{filePrg}
\newcounter{inputPrg}

\usepackage{geometry}
\geometry{paper=letterpaper,margin=2cm}

\usepackage{ifthen}

\usepackage{fontawesome}

\usepackage[dvipsnames]{xcolor}

\usepackage{tabularx}
\newcolumntype{\CeX}{>{\centering\let\newline\\\arraybackslash}X}%
\newcommand{\TwoSymbolsAndText}[3]{%
 \begin{tabularx}{\textwidth}{c\CeX c}%
   #1 & #2 & #3
 \end{tabularx}%
}

\usepackage{minted}

\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}

\newtcolorbox[use counter=commentCount, number format=\arabic]{commentBox}[2]{
 size=title,
 arc=1.5mm,
 breakable,
 enhanced jigsaw,
 colframe=Black,
 coltitle=White,
 boxrule=0.5mm,
 colback=White,
 coltext=Black,
 title=\TwoSymbolsAndText{\faCheck}{%
   \textbf{Comment \thetcbcounter}\ifthenelse{\equal{#1}{}}{}{\textbf{:} \textit{#1}}%
 }{\faCheck},
 label=comment:#2,
}

\newtcbinputlisting[use counter=filePrg, number format=\arabic]{\codeFromFile}[7]{%
 listing engine=minted,
 minted language=#1,
 minted style=trac,
 listing file={#2},
 minted options={autogobble,linenos,breaklines,firstline=#5,lastline=#6,highlightlines=#7},
 listing only,
 size=title,
 arc=1.5mm,
 breakable,
 enhanced jigsaw, 
 colframe=blue,
 coltitle=White,
 boxrule=0.5mm,
 colback=white,
 coltext=Black,
 title=\TwoSymbolsAndText{\faCode}{%
   \textbf{File program \thetcbcounter}\ifthenelse{\equal{#3}{}}{}{\textbf{:} \textit{#3}}%
 }{\faCode},
 label=filePrg:#4
}

\newtcblisting[use counter=inputPrg, number format=\arabic]{codeInput}[3]{
 listing engine=minted,
 minted language=#1,
 minted options={autogobble,linenos,breaklines},
 listing only,
 size=title,
 arc=1.5mm,
 breakable,
 enhanced jigsaw,
 colframe=brown,
 coltitle=White,
 boxrule=0.5mm,
 colback=white,
 coltext=Black,
 title=\TwoSymbolsAndText{\faCode}{%
   \textbf{Input program \thetcbcounter}\ifthenelse{\equal{#2}{}}{}{\textbf{:} \textit{#2}}%
 }{\faCode},
 label=inputPrg:#3
}

\usepackage{cleveref}
\crefname{commentCount}{comment}{comments}
\crefname{filePrg}{file program}{file programs}
\crefname{inputPrg}{input program}{input programs}

% * Begin document
\begin{document}

% * [1] Comment Box
 \begin{commentBox}{My first comment}{first}
   My first comment here! \faTree
 \end{commentBox}

% * [2] Code Input
 \begin{codeInput}{c}{A welcome program.}{code01}
   #include <stdio.h>
   int main() {
     printf("Hello world!\n");
     return 0;
   }
 \end{codeInput}

% * [3] Code from file
 \codeFromFile{python}{pytest.py}{Another welcome program.}{code01}{1}{25}{{5,8,11}}

% * [4] Cross reference
 See \cref{comment:first} for details. Also, look for \cref{inputPrg:code01} and \cref{filePrg:code01} for further assistance.

% * End document
\end{document}
  • 在minted options中设置显示行号及高亮
minted options={autogobble,linenos,breaklines,firstline=#5,lastline=#6,highlightlines=#7},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GuokLiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值