请移步:抄录环境中跨越多行的大括号,https://latexstudio.net/index/details/index/mid/1794.html
\documentclass{ctexart}
% 页面设置
\usepackage{geometry}
\geometry{a4paper,height=235mm,width=156mm}
\usepackage{tikz}
\usetikzlibrary{tikzmark,decorations.pathreplacing,calligraphy}
\usepackage{listings}
\usepackage{xurl}
\usepackage{hyperref}
\begin{document}
\title{抄录环境中跨越多行的大括号}
\author{zhangsming@foxmail.com}
\date{2021/08/03}
\maketitle
本文是 \LaTeX 工作室网站上问答栏目“如何在代码环境中插入跨越多行的花括号?”问题(\url{https://ask.latexstudio.net/ask/question/3646.html})的一种实现方法。
实现代码如下:
\begin{verbatim}
%\usepackage{listings}
\lstset {
,numbers = left
,numberstyle=\tiny
,backgroundcolor=\color{lightgray!50}
,basicstyle=\ttfamily
,escapeinside= {!}{!}
}
\begin{lstlisting}
function gcd(p,q) {
if (q === 0) {!\tikzmark{a}!
return q;
}else{
let r = p % q;!\tikzmark{b}!
return gcd(q, r);
}
}
\end{lstlisting}
\begin{tikzpicture}[remember picture,overlay]
\path (pic cs:a) -|node(test){} (pic cs:b);
\draw[overlay,decorate,
decoration={calligraphic brace,amplitude=4mm},
ultra thick] ([xshift=.3333em,yshift=.5em]test.center) --
([xshift=.3333em]pic cs:b)
node[xshift=5mm,anchor=west,midway]
{抄录环境大括号测试};
\end{tikzpicture}
\end{verbatim}
实际排版效果如下:
%\usepackage{listings}
\lstset {
,numbers = left
,numberstyle=\tiny
,backgroundcolor=\color{lightgray!50}
,basicstyle=\ttfamily
,escapeinside= {!}{!}
}
\begin{lstlisting}
function gcd(p,q) {
if (q === 0) {!\tikzmark{a}!
return q;
}else{
let r = p % q;!\tikzmark{b}!
return gcd(q, r);
}
}
\end{lstlisting}
\begin{tikzpicture}[remember picture,overlay]
\path (pic cs:a) -|node(test){} (pic cs:b);
\draw[overlay,decorate,
decoration={calligraphic brace,amplitude=4mm},
ultra thick] ([xshift=.3333em,yshift=.5em]test.center) --
([xshift=.3333em]pic cs:b)
node[xshift=5mm,anchor=west,midway]
{抄录环境大括号测试};
\end{tikzpicture}
\end{document}