2

I need to prevent my TikZ overlay's font settings from inheriting the font settings at ship out. I know this is a grouping issue because the closing } for the localized font changes has not yet been reached at shipout.

So maybe another way to ask this is to say, "How can I make my TikZ overlay part of the global scope"?

Problem Clarified

In the example, you will see that the font size of the overlay on page 2 has been undesirably affected by the settings of shipout at page 2.

Reasoning

Let's say I want to create a draft mode called "Developer Mode" that I can turn on and off. This mode uses an overlay created by TikZ and placed on each page (at shipout) using atbegshi. Unfortunately, the font settings in effect at the time of shipout are inherited by the TikZ overlay. I could explicitly set every font setting, but I think the smarter way would be to isolate the scope somehow.

Code

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{tikz}
\usetikzlibrary{calc}% for calculating distance between points
\usepackage{tikzpagenodes}% get area nodes
\usepackage{atbegshi}% for shipout access
\usepackage{lipsum}% for dummy text
\definecolor{blueprint}{cmyk}{.85,.51,0,0}% For visual effect

\makeatletter% for \f@size \def\printfontsize{\f@size pt} \makeatother

\def\false{false} \def\officialbuild{false} \ifx\officialbuild\false \AtBeginShipout{\AtBeginShipoutUpperLeftForeground{\draftpage}}% use upperleftforeground to make (0,0) at north west corner as TikZ expects \fi

\def\draftpage{ \begin{tikzpicture}[overlay,remember picture] \tikzset{blueprintmeasurement/.style={draw,dashed,color=blueprint,<->,opacity=0.5}} \tikzset{blueprintborder/.style={draw,color=blueprint,opacity=0.5}} \tikzset{blueprinttext/.style={color=blueprint,opacity=0.5}} \node [blueprinttext,anchor=north east,font=\sffamily\bfseries] at ($(current page.north east)+(-2mm,-2mm)$) {{Developer Mode \printfontsize}}; % Draw Current Page Text Area Frame \draw [blueprintborder]% text area west let \p{pathID1} = ($(current page text area.south west)-(current page text area.north west)$) in (current page text area.north west) node [left,yshift=-1cm] {\pgfmathparse{veclen(\x{pathID1},\y{pathID1})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm} to (current page text area.south west); \draw [blueprintborder]% text area north let \p{pathID} = ($(current page text area.north east)-(current page text area.north west)$) in (current page text area.north west) node [above,xshift=1cm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm} to (current page text area.north east); \draw [blueprintborder] % text area east (current page text area.north east) to (current page text area.south east); \draw [blueprintborder] % text area south (current page text area.south east) to (current page text area.south west); % Margins \draw [blueprintmeasurement]% left margin let \p{pathID} = ($(current page text area.west)-(current page.west)$) in (current page.west) node [above,xshift=10mm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm} to (current page.west -| current page text area.west); \draw [blueprintmeasurement]% right margin let \p{pathID} = ($(current page text area.east)-(current page.east)$) in (current page.east) node [above,xshift=-10mm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm} to (current page.west -| current page text area.east); \draw [blueprintmeasurement]% top margin let \p{pathID} = ($(current page text area.north)-(current page.north)$) in (current page.north) node [left,yshift=-10mm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm} to (current page text area.north -| current page.north); \draw [blueprintmeasurement]% top margin let \p{pathID} = ($(current page text area.south)-(current page.south)$) in (current page.south) node [left,yshift=20mm] {\pgfmathparse{veclen(\x{pathID},\y{pathID})/1mm}\pgfmathprintnumber[precision=2]{\pgfmathresult} mm} to (current page text area.south -| current page.south); \end{tikzpicture} }

\begin{document} \section{Section of Lipsum in \textbackslash normalfont} \printfontsize\ \lipsum \section{Section of Lipsum in \textbackslash tiny} {\tiny\printfontsize\ \lipsum \lipsum} \section{Following tiny font} \lipsum[1] \end{document}

Output

enter image description here enter image description here

  • 2
    What's the problem in doing \newcommand\draftpage{\begingroup\normalfont\normalsize<picture>\endgroup}? – egreg Feb 22 '17 at 14:10
  • I suppose that would work. But like I said under "Reasoning", I think it is better to inherit the global settings at begin shipout e.g. storing the font settings at begin shipout in a global macro and inserting that into \draftpage\bgroup\injectglobalfontsettingshere<picture>\egroup. You just gave me an idea that I am not sure whether I can implement. – Jonathan Komar Feb 22 '17 at 14:29
  • 4
    there really is no access to the global scope. Page heading code has to do exactly as egreg suggests resetting fonts, colours catcodes, etc in case the page break happens in odd places like inside verbatim. – David Carlisle Feb 22 '17 at 14:38
  • 1
    but if your overlay doesn't depend on the page, you should be able to save it all in a box and drop a fixed box on each page. No need to get tikz to re-draw it every time. – David Carlisle Feb 22 '17 at 14:41
  • @DavidCarlisle Well that is a bummer. I suppose then that the scope immediately after \begin{document} might suffice as the base setting (it is not global, but it might do the trick). Thanks for the optimization tip. I need to investigate that. Do you mean using \savebox? – Jonathan Komar Feb 22 '17 at 14:43
  • yes savebox would do, if you use a box then it is not affected at all by any local font changes. – David Carlisle Feb 22 '17 at 14:45
  • @DavidCarlisle Wait a minute, of course my overlay depends on the page! It contains all of the page measurements :) I suppose if I knew in advance that all the pages will be the same, I could use a box, but I have all kinds of dimension changes within documents. – Jonathan Komar Feb 22 '17 at 15:18
  • @DavidCarlisle If I wanted to do it anyway, I would do \AtBeginShipout{\AtBeginShipoutUpperLeftForeground{\usebox{\draftpageoverlaybox}}} after setting up the box, but it is not working :/ Nevermind. Just found out that TikZ's overlay option and \savebox are incompatible: http://tex.stackexchange.com/questions/47998/repeat-scaled-version-of-slide-savebox-and-usebox-in-multi-slide-frame – Jonathan Komar Feb 22 '17 at 15:39
  • sure but if the frame doesn't depend on the page contents you don't need any of the tikz overlay machinery as you don't need to calculate coordinates in the page body. – David Carlisle Feb 22 '17 at 16:28

0 Answers0