我正在为使用 shell/bash 命令的动手实践研讨会编写 LaTeX 文档。我正在利用该listings
包来格式化代码块。
有些命令相当长,因此我使用breaklines
和breakatwhitespace
。
这是我的 MWC:
\documentclass{book}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0,0.9,0}
\lstset{%
columns=fullflexible,
aboveskip=5pt,
belowskip=10pt,
basicstyle=\small\ttfamily,
numbers=left,
numberstyle=\tiny\color{black!85},
stepnumber=1,
numbersep=13pt,
backgroundcolor=\color{black!5},
showspaces=false,
showstringspaces=false,
showtabs=false,
xleftmargin=20pt,
xrightmargin=10pt,
framesep=5pt,
framerule=3pt,
frame=leftline,
rulecolor=\color{darkgreen},
tabsize=2,
breaklines=true,
breakatwhitespace=true,
}
\begin{document}
\begin{lstlisting}
cd ~/
time velveth run_25 25 -fmtAuto -create_binary -shortPaired -separate SRR022863_1.fastq.gz SRR022863_1.fastq.gz -shortPaired2 -separate SRR022852_1.fastq.gz SRR022852_2.fastq.gz
time velvetg run_25
\end{lstlisting}
\end{document}
显示为:
但是,我希望列表\
在自动换行的行尾包含一个行继续符 ( )。列表有办法实现这一点吗?我希望列表看起来像这样(缩进行上有或没有行号),而无需明确输入反斜杠:
答案1
您可以使用在当前行末尾prebreak=<tokens>
插入:<tokens>
\documentclass{book}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0,0.9,0}
\lstset{%
columns=fullflexible,
aboveskip=5pt,
belowskip=10pt,
basicstyle=\small\ttfamily,
numbers=left,
numberstyle=\tiny\color{black!85},
stepnumber=1,
numbersep=13pt,
backgroundcolor=\color{black!5},
showspaces=false,
showstringspaces=false,
showtabs=false,
xleftmargin=20pt,
xrightmargin=10pt,
framesep=5pt,
framerule=3pt,
frame=leftline,
rulecolor=\color{darkgreen},
tabsize=2,
breaklines=true,
breakatwhitespace=true,
prebreak=\small\symbol{'134}
}
\begin{document}
\begin{lstlisting}
cd ~/
time velveth run_25 25 -fmtAuto -create_binary -shortPaired -separate SRR022863_1.fastq.gz SRR022863_1.fastq.gz -shortPaired2 -separate SRR022852_1.fastq.gz SRR022852_2.fastq.gz
time velvetg run_25
\end{lstlisting}
\end{document}
根据使用的字体,最好替换
prebreak=\small\symbol{'134}
和
prebreak=\textbackslash
使用 Cork 编码加载 fontenc 后:
\usepackage[T1]{fontenc}
完整示例:
\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{color}
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0,0.9,0}
\lstset{%
columns=fullflexible,
aboveskip=5pt,
belowskip=10pt,
basicstyle=\small\ttfamily,
numbers=left,
numberstyle=\tiny\color{black!85},
stepnumber=1,
numbersep=13pt,
backgroundcolor=\color{black!5},
showspaces=false,
showstringspaces=false,
showtabs=false,
xleftmargin=20pt,
xrightmargin=10pt,
framesep=5pt,
framerule=3pt,
frame=leftline,
rulecolor=\color{darkgreen},
tabsize=2,
breaklines=true,
breakatwhitespace=true,
prebreak=\textbackslash
}
\begin{document}
\begin{lstlisting}
cd ~/
time velveth run_25 25 -fmtAuto -create_binary -shortPaired -separate SRR022863_1.fastq.gz SRR022863_1.fastq.gz -shortPaired2 -separate SRR022852_1.fastq.gz SRR022852_2.fastq.gz
time velvetg run_25
\end{lstlisting}
\end{document}