1

(Sorry the title is not very good.)

I came across Centering full-page tikz images - with mid-document paper size change and its answer https://tex.stackexchange.com/a/55421/11820 which I use as a wrapper for fullpage tikz pictures. Now I want to put it into an environment, but this does not work for

\node at (current page.north west) { … }

as \node at (current page.north west) { would be at the beginning of the environment and the closing brace } at the end.

How can I "embrace" the whole environment content?

MWE (not working)

\newenvironment{tikzpicturebase}
{%
\begin{tikzpicture}[overlay,remember picture]
\node at (current page.north west) {%
\begin{tikzpicture}[overlay,yscale=-1]
}
{
\end{tikzpicture}
};
\end{tikzpicture}
}
white_gecko
  • 2,101

3 Answers3

2

Nested tikzpictures are unsupported and unneeded here, as far as I understand the goal. You can use \bgroup and \egroup to avoid the brace-balancing issue.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\newenvironment{foobar} {% \begin{tikzpicture}[remember picture, overlay] \node[anchor=north west, inner sep=0pt, outer sep=0pt, fill=red, fill opacity=0.3] at (current page.north west) \bgroup\ignorespaces }% {% \unskip\egroup; \end{tikzpicture}% }

\begin{document} \begin{foobar} \includegraphics[width=\paperwidth]{example-image-duck} \end{foobar} \end{document}

enter image description here

This technique is robust in that you can even use verbatim material (or other nasty beasts that change category codes) in the environment body. For instance:

\begin{foobar}
  \Huge \verb|@#{}!%|
\end{foobar}

enter image description here

frougon
  • 24,283
  • 1
  • 32
  • 55
1

The environ package has a \BODY macro that contains what you have fed to the environment. If I have understood your wishes correctly I think that this could work for you. :)

\documentclass{article}
\usepackage{tikz}
\usepackage{environ}

\NewEnviron{tikzpicturebase} { \begin{tikzpicture}[overlay, remember picture] \node at (current page.north west) {\BODY}; \end{tikzpicture} }

\begin{document} \begin{tikzpicturebase} Hello! I am in the upper left corner! :) \end{tikzpicturebase} \end{document}

1

You can not nest tikzpictures — dragons will be there. But if you have a new enough latex, you can "grab" the whole environment using the +b type of parameter, and you can nest a tikzpicture using a box to save it.

Full example:

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\NewDocumentEnvironment{foo}{O{north west} +b}{% \begin{tikzpicture}[overlay,remember picture] \node[anchor=#1] at (current page.#1) {#2}; }{% \end{tikzpicture}% } \newbox{\abox} \sbox{\abox}{\begin{tikzpicture} \drawred, ultra thick circle[radius=20pt]; \end{tikzpicture}% } \begin{document} \begin{foo} \includegraphics[width=5cm]{example-image-duck} \end{foo} \begin{foo}[center] \usebox{\abox} \end{foo} \end{document}

output from the snippet above

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 1
    \begin{foo} \verb|@#{}!%| \end{foo} makes TeX rather unhappy! :-) (ditto for the environ-based solution) – frougon Jun 05 '22 at 20:18
  • @frougon you're right, but in my defense the OP didn't ask for nasty catcode-changing content.... ;-) – Rmano Jun 05 '22 at 20:39
  • True, I was “just pointing out” 'cause I felt in minority against two argument-grabbing techniques. :) – frougon Jun 05 '22 at 20:59