# Using titleformat* with tikzpicture (leaving horizontal mode)?

I'd like to use the same code as in Using tikzpagenodes with crop package? - except with \titleformat*, not with \titleformat. Why? Because in the original code I got with the template for my real doc has just (say):

\titleformat*{\section}{\normalfont\Large\bfseries\color{gray}}


... and I'd like to add the thumb index without significantly changing that code or its output; and for \titleformat I need to know if shape is [hang], and what the { sep } argument is, before I can replicate correctly - and I have no idea how to retrieve those values.

So with the change to \titleformat* in the MWE below, I get:

! Package titlesec Error: Entered in horizontal mode.


... but if I keep pressing ENTER, the doc compiles, and it is seemingly what I want (though possibly not the same as the \titleformat output); on the image (click for full res), left is the \titleformat*, right is the \titleformat:

Via this post: tikzpicture without leaving vertical mode , I got to the following - it looks related to me, but I'm not sure:

I think the problem is not that TeX leaves vertical mode - if you capture the content into a \vbox, the "leave vertical mode" command is restricted locally to that box.

Yes, but if I do:

\titleformat*{\section}{%
\normalfont\Large\bfseries\color{gray}%
\tikzexternaldisable%
\vbox{\sectioncolouration{\Alph{section}}}%
\tikzexternalenable%
}


... then a new line is inserted; and

... I succeeded when I used \nointerlineskip in front of it.

... I tried \nointerlineskip, it didn't seem to work. The only thing not inserting a new line seems to be \vbox to 0pt {\sectioncolouration{\Alph{section}}} but then that messes up the layout in a different way.

So - how can I use a \titleformat* with a tikzpicture without getting the "horizontal mode" error? Alternately, how does one "leave" horizontal mode without artefacts (for some reason, there is no \leavehmode, only \leavevmode)?

\documentclass[10pt,twoside,openright]{book}

\usepackage[
paperwidth=15cm,
paperheight=20cm,
outer=2.5cm,
inner=2.5cm,
top=2.5cm,
bottom=2.5cm
]{geometry}
\usepackage[a4,center]{crop}   % crop line
\crop[cam,info]                % crop line

\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{external,calc}
\usepackage{tikzpagenodes}
%\usepackage{lua-visual-debug}

% \titleformat{\section}[hang]{\normalfont\Large\bfseries\color{black!90}}{\thesection}{1.5em}{\tikzexternaldisable\sectioncolouration{\Alph{section}}}[\tikzexternalenable]
\titleformat*{\section}{%
\normalfont\Large\bfseries\color{gray}%
\tikzexternaldisable%
\vbox{\sectioncolouration{\Alph{section}}}%
\tikzexternalenable%
}

\newcommand*\sectioncolouration[1]{%
\begin{tikzpicture}[baseline=(a.base), overlay, remember picture, inner xsep=0pt, outer sep=0pt]
\node  (a) {\phantom{\normalfont\Large\bfseries Xy}};
\ifx\stockwidth\undefined
\coordinate (b) at (0,0);
\else
\coordinate (b) at (.5\paperwidth-.5\stockwidth,.5\stockheight-.5\paperheight);
\fi
\begin{scope}
\path [fill=blue, fill opacity=.2, even odd rule] (current page.west |- a.north) -| (current page.east |- a.south) -| cycle ({$(current page text area.west) + (b)$} |- a.north) -| ({$(current page text area.east) + (b)$} |- a.south) -| cycle;
\ifodd\value{page}
\node at (a -| {$(current page text area.east)!1/2!(current page.east) + .5*(b)$}) {#1};
\else
\node at (a -| {$(current page text area.west)!1/2!(current page.west) + .5*(b)$}) {#1};
\fi
\end{scope}
\end{tikzpicture}}

\begin{document}
\frontmatter
\mainmatter

\part{Introduction}
\chapter*{The first chapter - without thinking anything else}

\newpage

\section{The first section - and some more text inside; and some more text inside; and some more text inside}

Blah, blah, blah...

\begin{tikzpicture}[remember picture,overlay]
\draw[thick] (current page text area.north east) rectangle (current page text area.south west) ;
\end{tikzpicture}

\newpage
\section{This section starts on an even page}
Some text\dots

\end{document}

• Well, \par is technically a \leavehmode, but it's not what you're looking for I'm afraid. – yo' Mar 30 '15 at 8:48
• Since \titleformat* has limitations, why don't you use \titleformat? – egreg Mar 31 '15 at 18:12

I'm not sure why you don't want to use \titleformat, given the limitations of \titleformat*.

Well, here's a way:

\documentclass[10pt,twoside,openright]{book}

\usepackage[
paperwidth=15cm,
paperheight=20cm,
outer=2.5cm,
inner=2.5cm,
top=2.5cm,
bottom=2.5cm
]{geometry}
\usepackage[a4,center]{crop}   % crop line
\crop[cam,info]                % crop line

\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{external,calc}
\usepackage{tikzpagenodes}
%\usepackage{lua-visual-debug}

% \titleformat{\section}[hang]{\normalfont\Large\bfseries\color{black!90}}{\thesection}{1.5em}{\tikzexternaldisable\sectioncolouration{\Alph{section}}}[\tikzexternalenable]
\titleformat*{\section}{%
\normalfont\Large\bfseries\color{gray}%
\tikzexternaldisable%
\toks0=\expandafter{\the\everypar}%
\everypar={\sectioncolouration{\Alph{section}}\everypar=\toks0}%
\tikzexternalenable%
}

\newcommand*\sectioncolouration[1]{%
\begin{tikzpicture}[baseline=(a.base), overlay, remember picture, inner xsep=0pt, outer sep=0pt]
\node  (a) {\phantom{\normalfont\Large\bfseries Xy}};
\ifx\stockwidth\undefined
\coordinate (b) at (0,0);
\else
\coordinate (b) at (.5\paperwidth-.5\stockwidth,.5\stockheight-.5\paperheight);
\fi
\begin{scope}
\path [fill=blue, fill opacity=.2, even odd rule] (current page.west |- a.north) -| (current page.east |- a.south) -| cycle ({$(current page text area.west) + (b)$} |- a.north) -| ({$(current page text area.east) + (b)$} |- a.south) -| cycle;
\ifodd\value{page}
\node at (a -| {$(current page text area.east)!1/2!(current page.east) + .5*(b)$}) {#1};
\else
\node at (a -| {$(current page text area.west)!1/2!(current page.west) + .5*(b)$}) {#1};
\fi
\end{scope}
\end{tikzpicture}}

\begin{document}
\frontmatter
\mainmatter

\part{Introduction}
\chapter*{The first chapter - without thinking anything else}

\newpage

\section{The first section - and some more text inside; and some more text inside; and some more text inside}

Blah, blah, blah...

\begin{tikzpicture}[remember picture,overlay]
\draw[thick] (current page text area.north east) rectangle (current page text area.south west) ;
\end{tikzpicture}

\newpage
\section{This section starts on an even page}
Some text\dots

\end{document}


• (+1) That's really horrible ;). – cfr Mar 31 '15 at 22:28