// If need be, close <section>s, but only at the end of the document (otherwise, dealt with at the beginning
// of the loop).
- while (!headerLevels.empty() && headerLevels.top() > Layout::NOT_IN_TOC) {
+ while (!headerLevels.empty() && headerLevels.top() != Layout::NOT_IN_TOC && headerLevels.top() > 1) {
docstring tag = from_utf8("</section>");
headerLevels.pop();
xs << XMLStream::ESCAPE_NONE << tag;