# [texhax] tabularx and titletoc/titlesec: harmful interaction?

Susan Dittmar Susan.Dittmar at gmx.de
Tue Jul 17 18:35:13 CEST 2012

Hi folks,

I must be doing something wrong, but can't find it (probably due to my lack
of in depth understanding of TeX).

I want to have a program create a document. The contents is hierarchically
structured, but with a depth in this structure that exceeds the usual
section/subsection/... structure. Some months ago on this mailing list I
read an idea how to solve that, which I used since then. It makes use of
titlesec and titletoc to define a free-form section hierarchy. Then it
defines an environment that knows about the current hierarchy level and
enters into the next level, adding the appropriate section/subsection/...
header.  This environment is the same no matter the current depth, keeping
track of the current depth via a counter (I called it mysecdepth).

All this works fine with ordinary text. But as soon as I add a tabularx
inside such a section, the depth tracking breaks down! Even though the
enclosing section reaches its end, and a new section on the same level as
the one containing the tabularx is started, this new section appears to be
a subsection of the one containing the tabularx!

(For an example, see end of this mail.)

I have the impression that using tabularx somehow changes the local change
of the counter mysecdepth into a global change. Is that the case? How can I
countermand this automatically?

Thanks a lot for any hints,

Susan

%%%%%%%%%% example: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass{article}
\usepackage{tabularx}

\makeatletter
\RequirePackage{titletoc}

\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}

\newcounter{sectionI}					\setcounter{sectionI}{0}	\renewcommand{\thesectionI}		{\arabic{sectionI}}
\newcounter{sectionII}[sectionI]		\setcounter{sectionII}{0}	\renewcommand{\thesectionII}	{\thesectionI.\arabic{sectionII}}

\titleclass{\sectionI}[0]	{straight}
\titleclass{\sectionII}		{straight}[\sectionI]

\titleformat{\sectionI}		[frame]{\sffamily\bfseries\upshape}{\filright\footnotesize\enspace NOTE \thetitle\enspace}{.5em}{\sffamily\bfseries\upshape\filcenter\large}[]
\titleformat{\sectionII}	[frame]{\sffamily\bfseries\upshape}{\filright\footnotesize\enspace NOTE \thetitle\enspace}{.5em}{\sffamily\bfseries\upshape\filcenter\large}[]

\titlespacing{\sectionI}	{0pt}{*1.5}{*1.5}
\titlespacing{\sectionII}	{0pt}{*1.5}{*1.5}

\titlecontents{sectionI}	[0em]{}{\thecontentslabel~}{\hspace*{-1.2em}}{\titlerule*[0.675pc]{.}\contentspage}[]
\titlecontents{sectionII}	[0em]{}{\thecontentslabel~}{\hspace*{-1.2em}}{\titlerule*[0.675pc]{.}\contentspage}[]

\newcounter{mysecdepth}
\setcounter{mysecdepth}{0}
\newenvironment*{mysection}{%
\begingroup%
\advance\value{mysecdepth} by 1 % local assignment (??)
\ifcase\value{mysecdepth}%
\immediate\write16{mysection: counter mysecdepth has value 0! This should not be possible!}%
\expandafter\sectionI%
\or\expandafter\sectionI%
\or\expandafter\sectionII%
\else%
\immediate\write16{mysection: No more hierarchy levels!}%
\expandafter\sectionII%
\fi%
}{%
\par%
\endgroup%
}%
\makeatother

\begin{document}

\begin{mysection}{first section}
text of first section

\begin{mysection}{first subsection of first section}
text of first subsection of first section
\end{mysection}
\end{mysection}

\begin{mysection}{second section}
text of second section
\end{mysection}

\begin{mysection}{third section}
text of third section

\begin{tabularx}{\linewidth}{X}
This is a tabularx \\
just to show my problem.\\
\end{tabularx}
\end{mysection}

\begin{mysection}{fourth section}
text of fourth section, which falsely was chosen to
be first subsection of section tree
\end{mysection}

\end{document}