3

Question

Why is TikZ not remembering the location of my node despite adding the remember picture parameter? (Also, how can I improve this question title?)

Expected Result

The blue line should be drawn from the bottom left of the node titletext to the bottom right of the node titlenumber.

Actual Result

The titlenumber is not remembered correctly, therefore the line is drawn halfway across the page.

Compiler

xelatex Version 3.1415926-2.5-0.9999.3

Code

\documentclass{article}
\usepackage{fontspec}
\usepackage[compact,explicit,noindentafter]{titlesec}
\usepackage[rightlabels,dotinlabels]{titletoc}
\usepackage{needspace}
\usepackage{tikz} % Absolute positioning, advanced vector graphics
\usepackage{anyfontsize}

\newcommand{\tikzsectitle}[2]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
    \node [text width=.9\textwidth] (titletext) {#1};
\end{tikzpicture}
}%
\newcommand{\tikzsecnumber}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
    \node [font=\Huge\fontsize{60}{70}\selectfont,anchor=east] (titlenumber) at (titletext.center) {#1};
\end{tikzpicture}
}%
\newcommand{\tikztitleline}{%
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0pt,outer sep=0pt}]
    \draw (titletext.south west |- titlenumber.south) -- (titlenumber.south east);
\end{tikzpicture}
}%

% TITLE FORMATTING
\titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{}{0pt}{\tikzsectitle{#1}\hfill\tikzsecnumber{\thesection}}[\tikztitleline]
\titlespacing{\section}{0pt}{40pt}{2.3ex plus .2ex}

\begin{document}
\section{Test}
\end{document}

Output

enter image description here

Notes

For the sake of completeness, I will note some of the issues with my code.

  • The yshift for the blue line does not work for this reason.
  • Note that your style could not work well if your title is taller than the number height. The blue line will override the title. – Sigur Feb 6 '15 at 13:04
  • 1
    Yep, I know that. That is an easy fix tho in the draw coordinates. Thanks for the heads up anyway. e.g. (\draw (titletext.south west) -- (titletext.south -| titlenumber.east);) – Jonathan Komar Feb 6 '15 at 13:17
  • 1
    Actually, that might not be the best solution as I am seeing now, because the line would have to depend on whichever node is physically lower on the page. – Jonathan Komar Feb 6 '15 at 13:31
3

Your code had some problems, so I propose you the following modification:

\documentclass{article}
\usepackage{fontspec}
\usepackage[compact,explicit,noindentafter]{titlesec}
\usepackage[rightlabels,dotinlabels]{titletoc}
\usepackage{needspace}
\usepackage{showframe}
\usepackage{tikz} % Absolute positioning, advanced vector graphics
\usepackage{anyfontsize}
\usetikzlibrary{calc}

\newcommand{\tikzsectitle}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
    \node [text width=.7\textwidth] (titletext) {#1};
\end{tikzpicture}
}%
\newcommand{\tikzsecnumber}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
    \node [overlay,anchor=east,font=\Huge\fontsize{60}{70}\selectfont,text width=0.1\textwidth] (titlenumber) at ( $ (titletext.west) + (\textwidth,0) $ ){#1};
\end{tikzpicture}
}%
\newcommand{\tikztitleline}{%
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0pt,outer sep=0pt}]
    \draw [yshift=-2ex] (titletext.south west |- titlenumber.south) -- (titlenumber.south east);
\end{tikzpicture}
}%

% TITLE FORMATTING
\titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{}{0pt}{\tikzsectitle{#1}\tikzsecnumber{\thesection}}[\tikztitleline]
\titlespacing{\section}{0pt}{40pt}{2.3ex plus .2ex}

\begin{document}
\section{Test}
some test text some test text some test text some test text some test text some test text some test text some test text some test text some test text
\end{document}

Changes:

  • You initially declared \tikzsectitle with 2 mandatory arguments:

    \newcommand{\tikzsectitle}[2]{...
    

    bit were using it just with one in \titleformat:

    \titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{}{0pt}{\tikzsectitle{#1}\tikzsecnumber{\thesection}}[\tikztitleline]
    

    and this obviously produced some strange results that were not immediately noticeable but were there (for example, if you suppressed the \hfill then the node for the number was not found).

  • The modified version of \tikzsecnumber

    \newcommand{\tikzsecnumber}[1]{%
    \begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
        \node [overlay,anchor=east,font=\Huge\fontsize{60}{70}\selectfont,text width=0.1\textwidth] (titlenumber) at ( $ (titletext.west) + (\textwidth,0) $ ){#1};
    \end{tikzpicture}
    }%
    

    I added the overlay option, text width (this is optional, but I wasn't sure of the desired setting here), changed the anchor and changed the placement of the node from (titletext.center) to ( $ (titletext.west) + (\textwidth,0) $ ) (the calc library was added), so now the number will be flushed to the right margin not depending on the width of the title.

  • The modified version of \titleformat:

    \titleformat{\section}[hang]{\needspace{6cm}\color{blue}\Huge\bfseries}{}{0pt}{\tikzsectitle{#1}\hfill\tikzsecnumber{\thesection}}[\tikztitleline]
    

    No need for that \hfill in the original code, since the positioning is done using the coordinates of nodes.

The result (I added showframe to have a visual guideline for the margins and changed the text width for the title for demostration pusposes):

enter image description here

  • 1
    @Sigur Sure. I am on it :) I am adding an explanation of the modification. – Gonzalo Medina Feb 6 '15 at 13:02
  • Your solution is partially sufficient, but I need that \hfill, because I might decide to change the text width of the titletext node to something less for stylistic reasons. Then my number won't be right-aligned. Also, I noticed that the number node seems to gain an inner sep on the right when I add \usepackage[margin=2cm]{geometry}. – Jonathan Komar Feb 6 '15 at 13:09
  • @macmadness86 No, you don't really need it. Let me propose an alternative. – Gonzalo Medina Feb 6 '15 at 13:10
  • Note: \section{I should also note that I am dealing with multiline headings so it might be a good idea to put some extra text into the heading title} I probably could put that into my original question, but then I have to regenerate and and upload an output... – Jonathan Komar Feb 6 '15 at 13:12
  • @macmadness86, see my warning on the original question. – Sigur Feb 6 '15 at 13:14
2

You are inserting the node at the wrong place. Change the anchor point at (titletext.center) to at (titletext.east)

\newcommand{\tikzsecnumber}[1]{%
    \begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]
    \node [font=\Huge\fontsize{60}{70}\selectfont,anchor=east] (titlenumber) at (titletext.east) {#1};
    \end{tikzpicture}
}%
  • That does not answer the question. The mystery of the node titlenumber's location is still there. The line is drawn to titlenumber.south east, not to titletext.center, right? So how does changing that fix it? – Jonathan Komar Feb 6 '15 at 12:34
  • @macmadness86, do you want the line below both nodes? More or less like from left margin to right margin? – Sigur Feb 6 '15 at 12:36
  • Yes, precisely. So based on your answer, it seems like the node coordinates of titlenum are at titletext.center, but because the titlenum is not an overlay, it gets pushed to the right by the text (and \hfill). When I attempt to draw a line to this new shoved over location, TikZ draws a line to the original coordinate before the "shove". Is that correct? – Jonathan Komar Feb 6 '15 at 12:37
  • 1
    @macmadness86 Yes, essentially that is what is going on. – Gonzalo Medina Feb 6 '15 at 12:51
  • I see now that I can remove at (titletext.east) altogether, but then I lose vertical control over the number with respect to the text. That is not an ideal solution. It could decide later to align the number with the baseline or bottom of the titletext node in a multiline heading title. – Jonathan Komar Feb 6 '15 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.