# Prevent \tikzmark command from introducing additional whitespace

I am using the answer to this question: Drawing tikz line in the margin for multiple pages to draw a colored line on the margin of my document.

I would like to have a possibility to comment out the environment definition and acquire the same document without the colored line. But when I replace the outline environment definition with an empty one, the document has a different alignment.

MWE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newenvironment{outline}{\ignorespaces}{\ignorespaces}

\newcounter{markid}
% Comment this out to disable the marking of text:
\renewenvironment{outline}{\tikzmark{begin\themarkid}}{\ignorespaces\tikzmark{begin\themarkid}}

\begin{document}
\section{Section}
\begin{outline}

\begin{figure}[h!]
\caption{caption}
\end{figure}

Paragraph1
\end{outline}
\end{document}


The example above has additional empty space when the \renewenvironment is uncommented.

The only solution I have come up with so far is to take care that the environments' begin/end tags are in one block with paragraphs. (Therefore it seems to me that the \tikzmark command makes LaTeX create an additional, empty paragraph.)

Is there any other solution which would ensure that the \tikzmark commands don't change the outline of the document?

To avoid this, there is a command \pgfmark which does the same as \tikzmark but without the box. As far as I can tell, it gets rid of the additional space in your document.
• Thanks for the suggestion, it already improves a lot (there are much fewer differences in the spacing). I have tested it a little and found one more case, when the \pgfmark{...} is between an \end{itemize} and \section{} -- there is still extra free space. Any suggestions? – Adam Sep 3 '18 at 22:31