大家好,我最近一直在尝试使用代码块LaTex
,我需要为代码块添加标题和标签,以便在单击文档中的相应链接时捕获它们。请参阅下面的 MWE:
\documentclass[12pt]{report}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage[newfloat]{minted}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{subcaption}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{float}
\hypersetup{
colorlinks,
citecolor=electricultramarine,
filecolor=.,
linkcolor=maroon,
urlcolor=smokyblack,
linktoc=all
}
\definecolor{cobalt}{rgb}{0.0, 0.28, 0.67}
\definecolor{maroon}{rgb}{0.69, 0.19, 0.38}
\captionsetup[listing]{labelformat=empty,labelsep=none,justification=justified,singlelinecheck=false}
\begin{document}
...
For a description of the code used and specific versions for the tools, along with data availability, readers are referred to the appropriate section for the \hyperref[listing:samtools]{\textcolor{cobalt}{\textbf{Code and Data Availability}}} in the document.
...
\newpage
\begin{listing}[H]
\caption{Reads extraction from BAM files with \texttt{samtools 1.18-12-g147ac2f}.}
\begin{minted}[frame=lines,framesep=2mm,baselinestretch=1.2,
bgcolor=lightgray,fontsize=\footnotesize,linenos,breaklines]{bash}
# reads line by line a file list where paths for all samples are stored
NAMES=$1
d=$(sed -n "$SLURM_ARRAY_TASK_ID"p $NAMES)
# convert BAM to FASTQ and sort the latter for all samples
samtools fastq -c 9 --reference /leonardo_scratch/large/userexternal/mungaro0/hs37d5.fa -1 $d/R1.fastq.gz -2 $d/R2.fastq.gz -@ 32 $d/*.bam
samtools sort -n $d/R1.fastq.gz -o $d/R1_sorted.fastq.gz -@ 32
samtools sort -n $d/R2.fastq.gz -o $d/R2_sorted.fastq.gz -@ 32
rm $d/R1.fastq.gz $d/R2.fastq.gz
\end{minted}
\label{listing:samtools}
\end{listing}
\end{document}
现在这很棒,但是,一旦代码块跨越一页长度,我就不得不将其添加到序言中,按照一篇建议如何解决此问题的帖子:
\newenvironment{longlisting}{\captionsetup{type=listing}}{}
。
再次,问题已经解决,如果不是因为未满 \vbox 10000pt 在第 x 行;显然,源自背景颜色不适合这个目的...所以,我查了一下,发现\tcolorbox
对于这些情况来说,这是一个更好的选择,整体上代码块看起来更好,还有更多的功能,比如通过页面打破块。下面是使用 MWE 的\tcolorbox
:
\documentclass[12pt]{report}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage[newfloat]{minted}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{subcaption}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{float}
\hypersetup{
colorlinks,
citecolor=electricultramarine,
filecolor=.,
linkcolor=maroon,
urlcolor=smokyblack,
linktoc=all
}
\definecolor{cobalt}{rgb}{0.0, 0.28, 0.67}
\definecolor{maroon}{rgb}{0.69, 0.19, 0.38}
\captionsetup[listing]{labelformat=empty,labelsep=none,justification=justified,singlelinecheck=false}
\usepackage{tcolorbox}
\tcbuselibrary{listings, minted, xparse, breakable, skins}
\renewcommand{\theFancyVerbLine}{\textcolor[rgb]{1,1,1}{\scriptsize\arabic{FancyVerbLine}}}
\AtBeginDocument{%
\newtcblisting[blend into=listings]{bash}[2][]{%
title=#2,
detach title,
coltitle=black,
comment above* listing,
comment=\raggedright\tcbtitle,
breakable,
minted language=bash,
minted style=murphy, colback=lightgray, enhanced, frame hidden,
minted options={fontsize=\footnotesize, tabsize=2, breaklines, autogobble,
linenos, numbersep=5pt, frame=lines, framesep=2mm, baselinestretch=1.2},
overlay={
\begin{tcbclipinterior}\fill[black!75] (frame.south west)
rectangle ([xshift=5.1mm]frame.north west);\end{tcbclipinterior}
}
}
}
\begin{document}
...
For a description of the code used and specific versions for the tools, along with data availability, readers are referred to the appropriate section for the \hyperref[listing:samtools]{\textcolor{cobalt}{\textbf{Code and Data Availability}}} in the document.
...
\newpage
\begin{bash}{Reads extraction from BAM files with \texttt{samtools 1.18-12-g147ac2f}.}
# reads line by line a file list where paths for all samples are stored
NAMES=$1
d=$(sed -n "$SLURM_ARRAY_TASK_ID"p $NAMES)
# convert BAM to FASTQ and sort the latter for all samples
samtools fastq -c 9 --reference /leonardo_scratch/large/userexternal/mungaro0/hs37d5.fa -1 $d/R1.fastq.gz -2 %$d/R2.fastq.gz -@ 32 $d/*.bam
samtools sort -n $d/R1.fastq.gz -o $d/R1_sorted.fastq.gz -@ 32
samtools sort -n $d/R2.fastq.gz -o $d/R2_sorted.fastq.gz -@ 32
rm $d/R1.fastq.gz $d/R2.fastq.gz
\end{bash}
\end{document}
这非常棒,并且比之前的简单实现提供了更好的外观,但为了我,我找不到办法去除清单: #从标题中如示例所示\minted
...另外,我不知道如何添加可点击的标签...
最后,您可能已经注意到,这些是 BASH 代码块,出于某种原因,LaTex
对符号有抱怨$
,我无法弄清楚为什么或如何解决这个问题。您应该能够在 Overleaf 中测试所有内容,这就是我正在使用的。抱歉文字很长,任何帮助都非常感谢!
答案1
如果你希望你的链接可点击,你需要给链接添加标签
tcolorbox
如果您想删除
Listing #:
,可以使用blend before title code={}
。但是,如果您将其与正常列表混合,编号可能会非常混乱……
% !TeX program = txs:///arara
% arara: pdflatex: {synctex: on, interaction: nonstopmode, shell: yes}
\documentclass[12pt]{report}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage[newfloat]{minted}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{subcaption}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{float}
\hypersetup{
colorlinks,
citecolor=electricultramarine,
filecolor=.,
linkcolor=maroon,
urlcolor=smokyblack,
linktoc=all
}
\definecolor{cobalt}{rgb}{0.0, 0.28, 0.67}
\definecolor{maroon}{rgb}{0.69, 0.19, 0.38}
\usepackage{tcolorbox}
\tcbuselibrary{listings, minted, xparse, breakable, skins}
\AtBeginDocument{%
\newtcblisting[
blend into=listings
]{bash}[2][]{%
title=#2,
detach title,
coltitle=black,
comment above* listing,
comment=\raggedright\tcbtitle,
breakable,
minted language=bash,
minted style=murphy, colback=lightgray, enhanced, frame hidden,
minted options={fontsize=\footnotesize, tabsize=2, breaklines, autogobble,
linenos, numbersep=5pt, frame=lines, framesep=2mm, baselinestretch=1.2},
overlay={
\begin{tcbclipinterior}\fill[black!75] (frame.south west)
rectangle ([xshift=5.1mm]frame.north west);\end{tcbclipinterior}
},
blend before title code={},
#1
}
}
\begin{document}
...
For a description of the code used and specific versions for the tools, along with data availability, readers are referred to the appropriate section for the \hyperref[listing:samtools]{\textcolor{cobalt}{\textbf{Code and Data Availability}}} in the document.
...
\newpage
\begin{bash}[label=listing:samtools]{Reads extraction from BAM files with \texttt{samtools 1.18-12-g147ac2f}.}
# reads line by line a file list where paths for all samples are stored
NAMES=$1
d=$(sed -n "$SLURM_ARRAY_TASK_ID"p $NAMES)
# convert BAM to FASTQ and sort the latter for all samples
samtools fastq -c 9 --reference /leonardo_scratch/large/userexternal/mungaro0/hs37d5.fa -1 $d/R1.fastq.gz -2 %$d/R2.fastq.gz -@ 32 $d/*.bam
samtools sort -n $d/R1.fastq.gz -o $d/R1_sorted.fastq.gz -@ 32
samtools sort -n $d/R2.fastq.gz -o $d/R2_sorted.fastq.gz -@ 32
rm $d/R1.fastq.gz $d/R2.fastq.gz
\end{bash}
\end{document}