1

I have an environment (tikzpicture) that tries to occupy a height equal to the multiple of the current line height / baselineskip (I need this to be pixel-perfect, so in a multicol-setup the baselines match up in all columns before/after the image).

The fixed tikz height itself seems to work, but the environment itself does not keep a correct baseline-based spacing to the preceding paragraph. If letters extend below the baseline (e.g. a 'g'), the gap to the previous baseline is different than without extending characters.

different spacing before environment

How can I make sure the environment keeps an automatic baseline-based distance to the previous paragraph? I have many many uses of this environment in my document, so I do not want to manually add fixes based on the preceding content.

MWE:

\documentclass[openany]{book}
\usepackage{tikz}
\begin{document}

Test g.

\begin{tikzpicture}[y=\baselineskip]
    \draw (0, 0) -- (0, 4);
\end{tikzpicture}

Test

\begin{tikzpicture}[y=\baselineskip]
    \draw (0, 0) -- (0, 4);
\end{tikzpicture}

Test

\end{document}
1
  • My best solution so far is to output a \phantom{g} paragraph before the environment and then use a \vspace{-1\baselineskip} to position over it. Feels a bit like a hack though.
    – TeXter
    Commented Sep 30, 2021 at 13:30

1 Answer 1

3

This is not related with tikz, but with how TeX make paragraphs. Latex use the rubber length \baselineskip to set the distance between lines (plus \parskip if it is the beginning of a paragraph). But if you add a tikzpicture, or other box that make the line higher, then, if the lines would be too close together, closer than \lineskiplimit, then TeX instead uses \lineskip as the interline glue (see here), and this interline glue takes into account the depth of the previous line.

An example:

\documentclass[openany]{book}
\usepackage{tikz}

\setlength{\fboxsep}{-0.5pt}
\setlength{\fboxrule}{0.5pt}
%\setlength{\lineskip}{0cm}
%\setlength{\parskip}{1pt}

\newcommand{\tikzmark}[1]{\tikz[overlay, remember picture, baseline={(#1.base)}] 
\node[inner sep=0pt, line width=0pt] (#1) {\phantom{Tg}};}

\begin{document}

\fbox{Test}\fbox{g} \tikzmark{A}

\fbox{%
\begin{tikzpicture}[y=\baselineskip]
    \draw (0, 0) -- (1, 4);
\end{tikzpicture}} \tikzmark{B}

\fbox{Test} \tikzmark{C}

\fbox{\begin{tikzpicture}[y=\baselineskip]
    \draw (0, 0) -- (1, 4);
\end{tikzpicture}}

\fbox{Test} \tikzmark{D}

\fbox{Test}\fbox{g} \tikzmark{D2}

\fbox{Test} \tikzmark{D3}

\fbox{Test}

\begin{tikzpicture}[overlay, remember picture, font=\tiny]
    \draw[red] (A.south) -- node[right] {lineskip} ++(down:\lineskip);
    \draw[red, transform canvas={xshift=-0.43cm}] (B.base) -- node[right] {baselineskip} ++(down:\baselineskip);
    \draw[red] (C.base) -- node[right] {lineskip} ++(down:\lineskip);
    \draw[red] (D.base) -- node[right] {baselineskip} ++(down:\baselineskip);
    \draw[red] (D2.base) -- node[right] {baselineskip} ++(down:\baselineskip);
    \draw[red] (D3.base) -- node[right] {baselineskip} ++(down:\baselineskip);
\end{tikzpicture}

\end{document}

enter image description here

You can try remove the depth from the previous line (see here):

\documentclass[openany]{book}
\usepackage{tikz}

\setlength{\fboxsep}{-0.5pt}
\setlength{\fboxrule}{0.5pt}
%\setlength{\lineskip}{0cm}
%\setlength{\parskip}{1pt}

\newcommand{\tikzmark}[1]{\tikz[overlay, remember picture, baseline={(#1.base)}] 
\node[inner sep=0pt, line width=0pt] (#1) {\phantom{Tg}};}
\newcommand{\zerodepth}[1]{\raisebox{0pt}[\height][0pt]{#1}} 

\begin{document}

\fbox{Test}\fbox{\zerodepth{g}}

\fbox{%
\begin{tikzpicture}[y=\baselineskip]
    \draw (0, 0) -- (1, 4);
\end{tikzpicture}} 

\fbox{Test} 

\fbox{\begin{tikzpicture}[y=\baselineskip]
    \draw (0, 0) -- (1, 4);
\end{tikzpicture}}

\end{document}

enter image description here

but this solution is not going to work with whole paragraphs.

EDIT: Another option is add a \strut at the end of the line of paragraph avobe the tikz picture.

3
  • In my real document I use \setlength{\thelineheight}{13pt plus 0pt minus 0pt} to avoid the glue, but maybe that's not enough.
    – TeXter
    Commented Sep 30, 2021 at 13:32
  • Great analysis. Just noting that \tikzmark is now provided by the tikzmark library (what you use here is closest to \tikzmarknode from that package). Commented Oct 2, 2021 at 11:21
  • @TeXter I do not know \thelineheight length. I think that what you need is fix the glue between paragraphs to zero: \setlength{\parskip}{0cm} (removing the "rubber") Commented Oct 4, 2021 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .