在我的 LaTeX 文档中,我创建了一个侧边栏环境,其实现方式如下:
% Usage:
% \begin{sidebar}{title}
% text
% \end{sidebar}
\usepackage[framemethod=default]{mdframed}
\global\mdfdefinestyle{exampledefault}{%
linecolor=lightgray,linewidth=1pt,%
leftmargin=1cm,rightmargin=1cm,
}
\usepackage{float}
\newfloat{mdbox}{t}{box}
\floatname{mdbox}{Sidebar}
\newenvironment{sidebar}[1]{%
\mdfsetup{%
frametitle={\tikz\node[fill=white,rectangle,inner sep=0pt,outer sep=0pt]{#1};},
frametitleaboveskip=-0.5\ht\strutbox,
frametitlealignment=\raggedright
}%
\begin{mdbox}
\begin{mdframed}[style=exampledefault]
\setlength{\parskip}{0pt}
\setlength{\parindent}{2em}
}{
\end{mdframed}
\end{mdbox}}
这使得侧边栏很可爱,例如:
\begin{sidebar}{Lovely sidebar}
This is a lovely sidebar. This is a lovely sidebar. This is a
lovely sidebar. This is a lovely sidebar. This is a lovely
sidebar. This is a lovely sidebar. This is a lovely sidebar. This
is a lovely sidebar. This is a lovely sidebar. This is a lovely
sidebar. This is a lovely sidebar. This is a lovely sidebar. This
is a lovely sidebar. This is a lovely sidebar. This is a lovely
sidebar. This is a lovely sidebar. This is a lovely sidebar. This
is a lovely sidebar. This is a lovely sidebar.
\end{sidebar}
我想将侧边栏标题粘贴到标签中,以便可以在文本的其他地方引用它。但我不知道该怎么做。
答案1
在这里我加载nameref
包(通常由自动加载hyperref
)并将其添加到环境\def\@currentlabelname{#1}\NR@sanitize@labelname
定义的开头sidebar
。
经过这些更改,您可以使用\nameref
来引用 的标题sidebar
(如果它有标签的话)。
\documentclass{article}
\usepackage{nameref}
\usepackage{tikz}
\usepackage[framemethod=default]{mdframed}
\mdfdefinestyle{exampledefault}{
linecolor=lightgray,
linewidth=1pt,
leftmargin=1cm,
rightmargin=1cm,
}
\usepackage{float}
\newfloat{mdbox}{t}{box}
\floatname{mdbox}{Sidebar}
\makeatletter
\newenvironment{sidebar}[1]{%
\def\@currentlabelname{#1}%
\NR@sanitize@labelname
\mdfsetup{%
frametitle={\tikz\node[fill=white,rectangle,inner sep=0pt,outer sep=0pt]{#1};},
frametitleaboveskip=-0.5\ht\strutbox,
frametitlealignment=\raggedright
}%
\begin{mdbox}
\begin{mdframed}[style=exampledefault]
\setlength{\parskip}{0pt}
\setlength{\parindent}{2em}
}{
\end{mdframed}
\end{mdbox}}
\makeatother
\begin{document}
See sidebar \ref{key} on page \pageref{key} with title ``\nameref{key}''.
\begin{sidebar}{Lovely sidebar}\label{key}
This is a lovely sidebar. This is a lovely sidebar. This is a
lovely sidebar. This is a lovely sidebar. This is a lovely
sidebar. This is a lovely sidebar. This is a lovely sidebar. This
is a lovely sidebar. This is a lovely sidebar. This is a lovely
sidebar. This is a lovely sidebar. This is a lovely sidebar. This
is a lovely sidebar. This is a lovely sidebar. This is a lovely
sidebar. This is a lovely sidebar. This is a lovely sidebar. This
is a lovely sidebar. This is a lovely sidebar.
\end{sidebar}
\end{document}