Setting page background will influence the synctex location

backgroundsforward-inverse-search

I am using TeXstudio editor. When I set a background picture using the \addtohook command. It influence the Synctex locations always pointing to the end of the page not the actual text which I typed in.

Here is a simple example, I create the document that first page with background and second page without background:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\AddToHookNext{shipout/background}{%
    \put (0in,-\paperheight){\includegraphics{background.png}}%
}
\section{part 1}
This is the part one.
\section{part 2}
This is the part two.
\section{part 3}
This is the part three.
\section{part 4}
This is the part four.
\section{part 5}
This is the part five.
\section{part 6}
This is the part six.
\newpage
\section{part 7}
This is the part seven.
\section{part 8}
This is the part eight.
\section{part 9}
This is the part nine.
\section{part 10}
This is the part ten.
\section{part 11}
This is the part eleven.
\section{part 12}
This is the part twelve.
\end{document}

In the output pdf viewing, if you Ctrl+click the word in first page, it always pointing to the \newpage line. But in the second page, it actually pointing to the word I typed in. I have been tested using the background package, eso-pic package as well. They all giving same results. I think a workaround could be using a \colorbox to setting the background color but it won't fit to the whole page. Also it only allowed to setting simple color background not a actual picture background.

Is there a way to add background to the page, and make synctex still working properly?

enter image description hereI added the background picture in png file format, If anyone want to test it out. It was just a simple drawing using inkscape. The left and bottom side with gray color strip in the picture.

By further testing, I found the different formats of the picture files will also make a different on the synctex. If include a pdf file, every word will be not working. If include a png file, the section heading words will be not working. But the rest of texts will sync to the right locations unless it overlapped with the background. I can not upload the pdf picture file here. If anyone want to test it, you can create one using inkscape.

———————-Solution————————

After trying different methods, I found using tiz environment to add the background will not influence the synctex. I include the method here in case anyone have the some issue. In this way, the \addtohooknext command won't needed. Directly use the \begin{tikzpicture}. I think the 'remember picture' and 'overlay' options do the trick that everything within the current scope is not taken into consideration when the bounding box of the current picture is computed. See https://mirror.its.dal.ca/ctan/graphics/pgf/base/doc/pgfmanual.pdf p.259 about the details.

\begin{tikzpicture}[remember picture,overlay]
    \node[inner sep=0pt] at (current page.center)
    {\includegraphics[width=\paperwidth,height=\paperheight]{background.pdf}};
\end{tikzpicture}

Best Answer

The difference comes from the big box that is created by the \inludegraphics command that inserts your background image. LaTeX is organizing the page in horizontal boxes to contain lines and vertical boxes to contain paragraphs, lists, and much more. When you click in the pdf page, SyncTeX tries to find the closest box then brings you to the corresponding source.

On the first page with the background, if you click exactly on some text, smart editors like TeXworks brings you to the very letter you clicked on (TeXstudio will certainly do the same). But if you click a little away, then you fall back to the big background box that was created at shipout by the \newpage command: this is why you are redirected there. On the second page, if you click a little away, SyncTeX is given a chance to find a box nearby, and it seems to work better.

SyncTeX does not see pictures or text, it only sees boxes as containers (and some anchors as well). It does not see the format of an image, it only sees its size and location.

For the moment, there is no solution to your problem. Maybe with forthcoming LuaTeX facilities and some SyncTeX adaptation things can get better.