扩展:支持无标题的练习

扩展:支持无标题的练习

是否可以使用 ntheorem 的标题自动创建 PDF 书签?

例子:

\documentclass[a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[hyperref]{ntheorem}
\usepackage{lipsum}
\usepackage{hyperref}

%ntheorem
\makeatletter
\renewtheoremstyle{break}%
    {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
        ##1\ ##2\theorem@separator}\hbox{\strut}}}]}%
    {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
        ##1\ ##2:\ ##3\theorem@separator}\hbox{\strut}}}]}%
\makeatother

\theoremstyle{break}
\theoremheaderfont{\normalfont\bfseries\large}
\theorembodyfont{\normalfont}
\theoremseparator{\medskip}
\theorempostskip{\topsep\medskip}
\newtheorem{exercise}{Exercise}

\begin{document}

\begin{exercise}[Lorem]
\lipsum[10]
\end{exercise}

\begin{exercise}[Ipsum]
\lipsum[10]
\end{exercise}

\end{document}

我想要为每个练习环境创建一个书签。

在此示例中,书签应称为“练习 1:Lorem”和“练习 2:Ipsum”。因此一般为“练习 n:名称”。

不一定非要使用 hyperref,我不介意使用其他包。

答案1

  • 启动宏\exercise被修补以捕获可选参数。
  • hyperref已经为编号练习设置了锚点。书签直接使用此锚点。
  • 添加了软件包bookmark以实现上一点。此外,书签的更新速度也更快。

完整示例文件:

\documentclass[a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[hyperref]{ntheorem}
\usepackage{lipsum}
\usepackage{letltxmacro}

\usepackage[bookmarksopen=true]{hyperref}
\usepackage{bookmark}

%ntheorem
\makeatletter
\renewtheoremstyle{break}%
    {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
        ##1\ ##2\theorem@separator}\hbox{\strut}}}]}%
    {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
        ##1\ ##2:\ ##3\theorem@separator}\hbox{\strut}}}]}%
\makeatother

\theoremstyle{break}
\theoremheaderfont{\normalfont\bfseries\large}
\theorembodyfont{\normalfont}
\theoremseparator{\medskip}
\theorempostskip{\topsep\medskip}
\newtheorem{exercise}{Exercise}

\makeatletter    
\LetLtxMacro\old@exercise\exercise
\renewcommand*{\exercise}[1][]{%
  \old@exercise[{#1}]%
  \bookmark[dest=\@currentHref]{Exercise \theexercise: #1}%
}
\makeatother

\begin{document}

\begin{exercise}[Lorem]
\lipsum[10]
\end{exercise}

\begin{exercise}[Ipsum]
\lipsum[10]
\end{exercise}

\end{document}

结果

扩展:支持无标题的练习

\documentclass[a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[hyperref]{ntheorem}
\usepackage{lipsum}
\usepackage{letltxmacro}

\usepackage[bookmarksopen=true]{hyperref}
\usepackage{bookmark}

%ntheorem
\makeatletter
\renewtheoremstyle{break}%
    {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
        ##1\ ##2\theorem@separator}\hbox{\strut}}}]}%
    {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
        ##1\ ##2:\ ##3\theorem@separator}\hbox{\strut}}}]}%
\makeatother

\theoremstyle{break}
\theoremheaderfont{\normalfont\bfseries\large}
\theorembodyfont{\normalfont}
\theoremseparator{\medskip}
\theorempostskip{\topsep\medskip}
\newtheorem{exercise}{Exercise}

\makeatletter
\LetLtxMacro\old@exercise\exercise
\renewcommand*{\exercise}{%
  \@ifnextchar[{%
    \exercise@opt
  }{%
    \old@exercise
    \exercise@bookmark{}%
  }%
}
\def\exercise@opt[#1]{%
  \old@exercise[{#1}]%
  \exercise@bookmark{: #1}%
}
\newcommand*{\exercise@bookmark}[1]{%
  \bookmark[dest=\@currentHref]{Exercise \theexercise#1}%
}
\makeatother

\begin{document}

\begin{exercise}[Lorem]
\lipsum[10]
\end{exercise}

\begin{exercise}[Ipsum]
\lipsum[10]
\end{exercise}

\begin{exercise}
\lipsum[10]
\end{exercise}

\end{document}

附加练习结果(无标题)

相关内容