铸造:每个代码片段都有不同的“首位数字”

铸造:每个代码片段都有不同的“首位数字”

我用它minted来查看我的 bash 脚本。但是我必须将我的代码分成 4 个部分。我的问题是,现在每个代码段都以数字 1 开头。也就是说,我希望第 1 段从数字 1 到数字 10,第 2 段从数字 11 到数字 25 [...] 等等。我知道,这可以通过 来实现,firstnumber=# 但是我必须在代码的哪里写入它呢?当然,当我写入它\newmintedfile时,我的所有代码段都以这个数字开头。所以我必须把它放在

\bashscript{code/script.sh}

但是这里正确的语法是什么?

\documentclass[11pt,a4paper]{article}
\setlength{\parindent}{0pt}
\usepackage[english, ngerman]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage[usenames, dvipsnames]{xcolor}
\usepackage{minted}
\usepackage{caption}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\usetikzlibrary{shadings, backgrounds}
\usemintedstyle{perldoc}
\definecolor{mintedbackground}{rgb}{0.95,0.95,0.95}



\newmintedfile[bashscript]{bash}{
firstnumber=2,
bgcolor=mintedbackground,
fontfamily=tt,
linenos=true,
numberblanklines=true,
numbersep=5pt,
gobble=0,
frame=leftline,
framerule=0.4pt,
framesep=2mm,
funcnamehighlighting=true,
tabsize=4,
obeytabs=false,
mathescape=false
samepage=true, %with this setting you can force the list to appear on the same page
showspaces=false,
showtabs =false,
texcl=false,
}

\begin{document}

\bashscript{code/script1.sh}

\bashscript{code/script2.sh}

\end{document}

答案1

您可以使用该fancyvrb选项firstnumber=last

下面是一个例子:

\RequirePackage{filecontents}
\begin{filecontents}{script1.sh}
#!/bin/bash
# Simple line count example, using bash
#
# Bash tutorial: http://linuxconfig.org/Bash_scripting_Tutorial#8-2-read-file-into-bash-array
# My scripting link: http://www.macs.hw.ac.uk/~hwloidl/docs/index.html#scripting
#
# Usage: ./line_count.sh file
# -----------------------------------------------------------------------------

# Link filedescriptor 10 with stdin
exec 10<&0
# stdin replaced with a file supplied as a first argument
exec < $1
# remember the name of the input file
in=$1

# init
file="current_line.txt"
let count=0

# this while loop iterates over all lines of the file
while read LINE
do
    # increase line counter 
    ((count++))
    # write current line to a tmp file with name $file (not needed for counting)
    echo $LINE > $file
    # this checks the return code of echo (not needed for writing; just for demo)
    if [ $? -ne 0 ] 
     then echo "Error in writing to file ${file}; check its permissions!"
    fi
done
\end{filecontents}
\begin{filecontents}{script2.sh}
echo "Number of lines: $count"
echo "The last line of the file is: `cat ${file}`"

# Note: You can achieve the same by just using the tool wc like this
echo "Expected number of lines: `wc -l $in`"

# restore stdin from filedescriptor 10
# and close filedescriptor 10
exec 0<&10 10<&-
\end{filecontents}
\documentclass[11pt,a4paper]{article}
\setlength{\parindent}{0pt}
\usepackage[english, ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[usenames, dvipsnames]{xcolor}
\usepackage{minted}
\usepackage{caption}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\usetikzlibrary{shadings, backgrounds}
\usemintedstyle{perldoc}
\definecolor{mintedbackground}{rgb}{0.95,0.95,0.95}



\newmintedfile[bashscript]{bash}{
firstnumber=2,
bgcolor=mintedbackground,
fontfamily=tt,
linenos=true,
numberblanklines=true,
numbersep=5pt,
gobble=0,
frame=leftline,
framerule=0.4pt,
framesep=2mm,
funcnamehighlighting=true,
tabsize=4,
obeytabs=false,
mathescape=false
samepage=true, %with this setting you can force the list to appear on the same page
showspaces=false,
showtabs =false,
texcl=false,
}

\begin{document}

\bashscript{script1.sh}

\bashscript[firstnumber=last]{script2.sh}

\end{document}

相关内容