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 06 '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 06 '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 06 '15 at 13:31

2 Answers2

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

Gonzalo Medina
  • 505,128
  • 1
    @Sigur Sure. I am on it :) I am adding an explanation of the modification. – Gonzalo Medina Feb 06 '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 06 '15 at 13:09
  • @macmadness86 No, you don't really need it. Let me propose an alternative. – Gonzalo Medina Feb 06 '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 06 '15 at 13:12
  • @macmadness86, see my warning on the original question. – Sigur Feb 06 '15 at 13:14
  • Oh Gonzalo, I find myself in the same predicament as before in that unfortunate dialog with Ulrike. My MWE does not include it, but I am using landscape pages, where the current page nodes are currently messed up, at least until this question gets answered: http://tex.stackexchange.com/questions/226678/how-can-i-maintain-the-current-page-nodes-for-portrait-when-temporarily-entering – Jonathan Komar Feb 06 '15 at 13:15
  • @macmadness86 I see. Then forget this answer. I'll delete it. – Gonzalo Medina Feb 06 '15 at 13:17
  • I think it would be a good idea to keep it for other documents where that option is viable. For my current situation, however, the current page nodes are not working. I have to find a way to work with \hfill as I said before :( – Jonathan Komar Feb 06 '15 at 13:20
  • @macmadness86 Yes, I've re-edited my answer reintroducing the \hfill and forgetting about page nodes. What should happen to the blue line for longer titles? – Gonzalo Medina Feb 06 '15 at 13:21
  • @GonzaloMedina About the blue line: See my comment to Sigur the original question. The output of your new code looks great at first glance, but as soon as I change the text width of titletext, the number is no longer right-aligned. – Jonathan Komar Feb 06 '15 at 13:26
  • @macmadness86 I updated my answer :) Hopefully we are now getting closer to what you want to achieve. Just to be clear, the blue line should always go from the left margin to the left margin immediately below the tallest node? – Gonzalo Medina Feb 06 '15 at 13:47
  • Ok, definitely closer to the target. To clarify, the blue line should always be a specified distance (in this case -2ex, which is actually wrong because that y-shift should be in the tikzpicture parameters and not under \draw) from the lowest node (either titletext or titlenumber. The reason I worded it like that is because the line is drawn differently depending on whether there is a multiline heading title or not. – Jonathan Komar Feb 07 '15 at 13:48
  • @macmadness86 I am confused about the blue line. You mention it should be drawn at a specified distance, but where is the reference point? I mean, the line should be drawn 2ex below of what exactly? – Gonzalo Medina Feb 07 '15 at 17:27
  • You've identified the problem exactly. Originally, I thought the reference point should be the titlenumber node, but then I realized that this is a problem in a multiple line heading title. I do not know if there is a solution to this issue. – Jonathan Komar Feb 08 '15 at 09:13
  • The problem I mentioned above could be solved differently—perhaps wrapping the two nodes in a node, and using that as a reference node for the line. This would solve the problem of determining which node is physically lower. Anyway, I wish I could give you another +1 for finding a solution without \hfill despite the criterion that the current page nodes + overlay cannot be used. – Jonathan Komar Feb 08 '15 at 14:29
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}
}%
Sigur
  • 37,330
  • 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 06 '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 06 '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 06 '15 at 12:37
  • 1
    @macmadness86 Yes, essentially that is what is going on. – Gonzalo Medina Feb 06 '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 06 '15 at 12:52