3

I am using Miktex 2.9 with Texstudio 2.12.6. My document is in KOMA-class. As I was trying to load the optidef package, I got an error which seems related to \addcontentsline{toc}. I assumed some outdated packages to be the cause, so I have updated all packages using Miktex Updater. The problem remains.

Here is a 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}

Without optidef package, the document compiles flawlessly. As soon as I load the package optidef, I will get the following error message for all corresponding lines of code:

Undefined control sequence. \addtocontents{toc}{\setcounter{tocdepth}{0}}

Does anyone have a solution? Thank you very much in advance.

Best regards, Dan.

1 Answer 1

5

Add some \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}

Explanation: optidef requires package calc and the latter modifies \setcounter in a way which makes the \protect here needed.

3
  • but these lines do not make much sense anyhow especially one after the other
    – user4686
    Oct 7, 2018 at 12:07
  • Thanks for your solution. Indeed, these lines do not make any sense in the MWE. In my actual document, I am loading prefix chapters in between these lines, which does give them a real purpose :)
    – dan
    Oct 7, 2018 at 13:31
  • 1
    @dan Actually \protect should also be used without loading optidef.
    – egreg
    Oct 7, 2018 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .