1

Context

For a personal project I would like to have a tikzpicture on every page that borders the text and all that is on there. I started with LuaLaTeX and wanted to add only a few Chinese characters, and tested \usepackage[UTF8]{ctex}. I noticed how it unwantedly changed the spacing in the rest of my document. Then I tested XeLatex as the compiler and the corresponding xeCJK package, which seemed to do what I wanted. However, I noticed that the relative positioning with respect to the page that I had created while using the background package and an additional tikzpicture on top of that were in the incorrect positions. I used the code from the following post Positioning relative to page in TikZ and answer by 'romain.bqt4', such that the top-left corner of the page has coordinates (0, 0) and the bottom-right has coordinates (1, 1). Then I Googled the problem and it seemed to be that XeLaTeX engine does not work well with the overlay option in \begin{tikzpicture}. Also I found out that nested tikzenvironments are not 'the way to go', so I am trying to implement this properly. I think I fixed most of the issues I had, but then came across 'Dimension too large.' at every \newpage command. A very inconsistent error, as while I tried to provide code for a MWE, recompiling the code does not give the errors all the time. It seems that using the \input command somehow causes this, as I am trying around now. See the code for a (hopefully) MWE.

Code

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{background}
\usepackage[T1]{fontenc}
\usepackage{xeCJK}
\setCJKmainfont{NotoSansSC-Bold.otf}
\usepackage{lipsum}
\usepackage[textwidth=24cm, centering, margin=2cm, showframe]{geometry}
\usetikzlibrary{shapes.misc, positioning, calc}

\SetBgAngle{0} \SetBgScale{1} \SetBgOpacity{1} \SetBgContents{ \begin{tikzpicture}[remember picture,shift=(current page.north west)] % for XeLaTeX \begin{scope}[x={(current page.north east)},y={(current page.south west)}] \node (logo) at (0.5, 0.95) {\pic}; \draw[rounded corners=10pt, line width = 2pt] (logo) -| (0.05, 0.025) -- (0.95, 0.025) |- (logo); \end{scope} \end{tikzpicture} }

\newcommand*{\pic}{\includegraphics[height = 2cm]{example-image.pdf}}

\pagestyle{empty} \begin{document}

% The 'nested' tikzpicture \input{mwe} % The following lines are in mwe.tex % \begin{tikzpicture}[remember picture,overlay,shift=(current page.north west)] % \begin{scope}[x={(current page.north east)},y={(current page.south west)}]

% \node at (1/2, 1/2) {你好};

% \node at (1/5, 1/5) {\pic}; % \node at (2/5, 1/5) {\pic}; % \node at (3/5, 1/5) {\pic}; % \node at (4/5, 1/5) {\pic};

% \node at (1/5, 2/5) {\pic}; % \node at (2/5, 2/5) {\pic}; % \node at (3/5, 2/5) {\pic}; % \node at (4/5, 2/5) {\pic};

% \node at (1/5, 3/5) {\pic}; % \node at (2/5, 3/5) {\pic}; % \node at (3/5, 3/5) {\pic}; % \node at (4/5, 3/5) {\pic};

% \draw[dashed, line width = 2pt] (0.05, 0.785) -- (0.95, 0.785); % \node at ({1/3}, 0.855) {\pic}; % \node at ({2/3}, 0.855) {\pic};

% \end{scope} % \end{tikzpicture} \newpage \lipsum[1] \newpage \lipsum[2] \end{document}

Output

This is the output I would like to have and it seems to work. The output I want to have Errors

Question

What causes these dimension errors and why did I not see them when using LuaLaTeX?

PLY
  • 149
  • 9
  • Minor point, \pic is a tikz command so you probably shouldn't use it as a custom command. – Andrew Stacey Apr 22 '22 at 11:50
  • 1
    @AndrewStacey oops you're completely right, will keep it in mind! – PLY Apr 22 '22 at 11:55
  • Using \AddToHook{shipout/background}{\put(0pt,-\paperheight) {...}} will put the contents at the lower left corner of the page. No package needed. – John Kormylo Apr 22 '22 at 15:08
  • @JohnKormylo I am trying this command now and it does put the content on the lower left of the page. How would I get this centered as I have it in the output in the post? – PLY Apr 25 '22 at 09:24

1 Answers1

3

There are few things, I should mention:

  1. \usepackage[utf8]{inputenc} is no longer needed, as utf8 encoding is the default setting since April 2018 (see https://tug.org/TUGboat/tb39-1/tb121ltnews28.pdf)

  2. If you use XeCJK package and XeLaTeX compiler, package fontspec will automatic loaded. And \usepackage[T1]{fontenc} is not needed as well. You should use the command \setmainfont{}, \setsansfont{} and \setmonofont{} offered by fontspec package to set your English font. Similar as you set your Chinese font, e.g. \setCJKmainfont{NotoSansSC-Bold.otf}

  3. The default paper size for article class is the letter paper (11in height X 8.5in width), if you convert to cm it will be about 27.94cm X 21.59cm. So you can not set textwidth=24cm and margin=2cm as the page layout. The textwidth will larger than total paperwidth, latex will ignore it. I think you get the warning massage from latex when you do this.

  4. As mention in the previous comments, background package is not needed, because the latex hook system can basic do the same thing by using \AddToHook{shipout/background}{...any latex code...}. And be careful about naming a new command to make sure it not be defined by latex or other packages. So \pic need to be change to something else.

I change the code according to these points. And it should looks like what you wanted. If any of my statements were not correct or accurate, please correct me. There is the code:

\documentclass{article}
\usepackage{tikz}
\usepackage{xeCJK}
\setCJKmainfont{NotoSansSC-Bold.otf}
\usepackage{lipsum}
\usepackage[centering,margin=2cm,showframe]{geometry}
\usetikzlibrary{shapes.misc, positioning, calc}

\AddToHook{shipout/background}{ \begin{tikzpicture}[remember picture,overlay] \node at (current page.north west) { \begin{tikzpicture}[remember picture,overlay] \node (logo) at (0.5\paperwidth, -0.95\paperheight) [anchor=center,inner sep=0pt] {\addpicture}; \draw[draw=red!45,anchor=north west,rounded corners=10pt, line width = 2pt] (logo) -| (0.05\paperwidth, -0.025\paperheight) -- (0.95\paperwidth, -0.025\paperheight) |- (logo); \end{tikzpicture} }; \end{tikzpicture} }

\newcommand*{\addpicture}{\includegraphics[height = 2cm]{example-image.pdf}}

\pagestyle{empty} \begin{document}

% The 'nested' tikzpicture \input{mwe} % The following lines are in mwe.tex

\newpage \lipsum[1] \newpage \lipsum[2] \end{document}

% mwe.tex
\begin{tikzpicture}[remember picture,overlay,shift=(current page.north west)]
\begin{scope}[x={(current page.north east)},y={(current page.south west)}]
\node at (1/2, 1/2) {你好};
\node at (1/5, 1/5) {\addpicture};
\node at (2/5, 1/5) {\addpicture};
\node at (3/5, 1/5) {\addpicture};
\node at (4/5, 1/5) {\addpicture};
\node at (1/5, 2/5) {\addpicture};
\node at (2/5, 2/5) {\addpicture};
\node at (3/5, 2/5) {\addpicture};
\node at (4/5, 2/5) {\addpicture};
\node at (1/5, 3/5) {\addpicture};
\node at (2/5, 3/5) {\addpicture};
\node at (3/5, 3/5) {\addpicture};
\node at (4/5, 3/5) {\addpicture};
\draw[dashed, line width = 2pt] (0.05, 0.785) -- (0.95, 0.785);
\node at ({1/3}, 0.855) {\addpicture};
\node at ({2/3}, 0.855) {\addpicture};
\end{scope}
\end{tikzpicture}
Tom
  • 7,318
  • 4
  • 21
  • Sorry for the late response! It seems to do what I want to, but could you edit the code so that we don't need the \paperwidth and \paperheight commands for the hook? Or is that not possible? Why does this not need to be added to the mwe.tex? I am missing some information such as why this solves the dimension error. If that can get answered I will gladly award the bounty. – PLY Apr 28 '22 at 21:30
  • They are basically do the same thing. Just different method. I recommend you take a look the pgfmanual, https://muug.ca/mirror/ctan/graphics/pgf/base/doc/pgfmanual.pdf, Page 375 – Tom Apr 29 '22 at 00:31
  • I think the dimension error is caused by using the background package. Background package internally use tikz to draw the background. So when you write some code in \SetBgContents{}. You basically put a tikzpicture inside another tikzpicture, but you didn't use the overlay option in your tikzpicture that is the reason to cause the dimension error too large. See pgfmanual Page 259 about the remember picture and overlay option. – Tom Apr 29 '22 at 01:59