我正在使用 Miktex 2.9 和 Texstudio 2.12.6。我的文档属于KOMA
-class。当我尝试加载optidef
包时,我收到一个似乎与 相关的错误\addcontentsline{toc}
。我假设一些过时的包是原因,所以我使用 Miktex Updater 更新了所有包。问题仍然存在。
以下是 MWE:
\documentclass[12pt, twoside, openright, numbers=noenddot]{scrreprt}
%%%%%%%%%%%%%%%%% BEGIN PACKAGES
%%% Page header
\usepackage[automark,headsepline=off]{scrlayer-scrpage}
%%% Mathematic equations
%\usepackage{optidef}
%%% links in pdf
\usepackage{hyperref}
\usepackage[all]{hypcap}
\usepackage[numbered]{bookmark}
%%%%%%%%%%%%%%%%% END PACKAGES
\begin{document}
%%% ToC
\pagenumbering{roman}
\phantomsection
\pdfbookmark{Inhaltsverzeichnis}{toc}
\setcounter{tocdepth}{4}
\tableofcontents
%%%% from here on: add elements to ToC
\addtocontents{toc}{\setcounter{tocdepth}{0}}
%%%% chapters
\addtocontents{toc}{\setcounter{tocdepth}{4}}
\chapter{Test}
test
%%%% Optidef Example
%\begin{mini}[2]
% {w}{f(w)+ R(w+6x)}
% {\label{eq:Example1}}
% {}
% \addConstraint{g(w)}{=0}
% \addConstraint{n(w)}{= 6}
% \addConstraint{L(w)+r(x)}{=Kw+p}
% \addConstraint{h(x)}{=0.}
%\end{mini}
\end{document}
如果没有optidef
包,文档编译起来没有任何问题。但只要我加载包optidef
,所有相应的代码行都会出现以下错误信息:
未定义控制序列。\addtocontents{toc}{\setcounter{tocdepth}{0}}
有人有解决方案吗? 提前谢谢您。
谨致问候,丹。
答案1
添加一些\protect
:
\documentclass[12pt, twoside, openright, numbers=noenddot]{scrreprt}
%%%%%%%%%%%%%%%%% BEGIN PACKAGES
%%% Page header
\usepackage[automark,headsepline=off]{scrlayer-scrpage}
%%% Mathematic equations
\usepackage{optidef}
%%% links in pdf
\usepackage{hyperref}
\usepackage[all]{hypcap}
\usepackage[numbered]{bookmark}
%%%%%%%%%%%%%%%%% END PACKAGES
\begin{document}
%%% ToC
\pagenumbering{roman}
\phantomsection
\pdfbookmark{Inhaltsverzeichnis}{toc}
\setcounter{tocdepth}{4}
\tableofcontents
%%%% from here on: add elements to ToC
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
%%%% chapters
\addtocontents{toc}{\protect\setcounter{tocdepth}{4}}
\chapter{Test}
test
%%%% Optidef Example
\begin{mini}[2]
{w}{f(w)+ R(w+6x)}
{\label{eq:Example1}}
{}
\addConstraint{g(w)}{=0}
\addConstraint{n(w)}{= 6}
\addConstraint{L(w)+r(x)}{=Kw+p}
\addConstraint{h(x)}{=0.}
\end{mini}
\end{document}
解释:optidef
需要包calc
,并且后者\setcounter
以某种方式进行修改,使得\protect
这里需要。