2

I use checkifodd and varialbe x, y.

When compiling, error: Something went wrong while rendering this PDF

My code:

\documentclass{article}
\usepackage{datatool}
\usepackage{filecontents}
\usepackage[strict]{changepage}
\usepackage{ifoddpage}
\usepackage{tcolorbox}
\usepackage[absolute,overlay]{textpos}
\usepackage{tikz}
\begin{filecontents*}{test1.csv}
Acol, Bcol,Ccol
Ax,Bx,1
Ay,By,3
A1,B22,1
A2,B44,2
A3,B11,3
\end{filecontents*}

\DTLloaddb{mydata1}{test1.csv}

\newcommand*{\x}{\ifoddpage 5cm\else 10cm\fi}
\newcommand*{\y}{\ifoddpage 1cm\else 6cm\fi}

\begin{document}

\DTLforeach*{mydata1}{\A=Acol,\B=Bcol,\C=Ccol}%
{%
\checkoddpage{

\begin{tikzpicture}[remember picture,overlay]
\node (n1)[anchor=north west,xshift=\x,yshift=\y] at (current page.north west) {\includegraphics[height=5cm]{example-image-b}};

\end{tikzpicture}

\newpage

}
}
\end{document}

Please help Thanks

2

I wouln't use both the ifoddpage and changepage packages in the same document without first making sure they are compatible, because they both provide at least one command with the same name (namely \checkoddpage, but see the note below). I'm sticking with changepage here, because it is the one I used in my previous answers to you on these subjects.

Note: regarding this compatibility, the documentation of the ifoddpage package says:

The changepage package and the memoir class also provide the core functionality of this package, i.e. they define \checkoddpage and \ifoddpage but no other of the mentioned macros. That package uses this conditional internally in order to allow the user to change the page layout for odd or even pages, which is its main function. The ifoddpage [package] should work together with that package and class, but should be loaded last to ensure the correct functionality of \ifoddpageoroneside. It also uses labels if it is loaded using the strict option.

That said, the real problem in your document is that in order for your \x and \y macros to produce a 〈dimen〉 (a rigid length), TeX needs to perform two expansion steps, but TikZ appears to do only one for things written in the optional argument of \node. For instance, you can verify that if you do the first expansion step yourself, it works:

\checkoddpage
\begin{tikzpicture}[remember picture,overlay]
  \node (n1) [anchor=north west, xshift=\ifoddpage 5cm\else 10cm\fi,
              yshift=\ifoddpage 1cm\else -6cm\fi]
    at (current page.north west)
    {\includegraphics[height=5cm]{example-image-b}};
\end{tikzpicture}

\ifoddpage 5cm\else 10cm\fi expands to either 5cm or 10cm in one expansion step, and this does work in the optional argument of \node. But when you use \x, you need one expansion step to obtain \ifoddpage 5cm\else 10cm\fi and a second one to obtain either 5cm or 10cm. The TikZ parser doesn't seem to do the two steps (how could it guess how many are needed?).

In order to nicely solve this problem, my suggestion would be to:

  • first execute commands such as \edef or \pgfmathsetmacro that perform full expansion before the \node command in order to define two macros \myxshift and \myyshift that need only one expansion step to yield the desired result for the current page;

  • then use \myxshift and \myyshift in the \node options.

\documentclass{article}
\usepackage{datatool}
\usepackage{filecontents}
\usepackage[strict]{changepage}
\usepackage{tikz}

\begin{filecontents*}{test1.csv}
Acol, Bcol,Ccol
Ax,Bx,1
Ay,By,3
A1,B22,1
A2,B44,2
A3,B11,3
\end{filecontents*}

\DTLloaddb{mydata1}{test1.csv}

\newcommand*{\x}{\ifoddpage 5cm\else 10cm\fi}
% I changed 6cm to -6cm here, otherwise the image is invisible (above the
% page!).
\newcommand*{\y}{\ifoddpage 1cm\else -6cm\fi}

\begin{document}

\DTLforeach*{mydata1}{\A=Acol,\B=Bcol,\C=Ccol}%
{%
\checkoddpage
\begin{tikzpicture}[remember picture,overlay]
  \edef\myxshift{\x}            % or \pgfmathsetmacro{\myxshift}{\x}
  \edef\myyshift{\y}            % or \pgfmathsetmacro{\myyshift}{\y}

  \node (n1) [anchor=north west, xshift=\myxshift, yshift=\myyshift]
    at (current page.north west)
    {\includegraphics[height=5cm]{example-image-b}};
\end{tikzpicture}

\newpage
}
\end{document}

Pages

2
  • thank for you awesom solution. Do you have other solution that can definition "\edef\myxshift{\x} % or \pgfmathsetmacro{\myxshift}{\x}" before begin{document}? thanks
    – latexforti
    Jul 31 '19 at 9:59
  • 1
    I'm afraid not. The result of these commands depend on the last \checkoddpage check. If you do them earlier, the result will be wrong.
    – frougon
    Jul 31 '19 at 10:01

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.