I am trying to distribute a number of tikz
elements/groups consisting of one \pic
(= an icon) and one text \node
(= label) each evenly across the length of the text area.
I went about it by using a discard
layer to draw dummies of my elements on it, measuring their widths, accumulatively subtracting them from \textwidth
and then dividing by the number of elements/groups to get the seperation between my groups. Then I use the positioning
tikz-library together with the precalculated seperation length to place my groups. But for some reason this doesn't lead to my groups actually filling the whole width of the text area.
Here is a simplyfied MWE, where I also introduced a test length (visulized by the read line added to the picture) which seems to verify that all the parts of my calculation actually add up to \textwidth
. Yet my groups don't fill the whole width of the text area:
\documentclass[12pt,a4paper,ngerman,oneside]{scrlttr2}
\def\fontscalingfac{1}
% \renewcommand{\fontscalingfac}{0.85}
\usepackage[sfdefault,scaled=\fontscalingfac]{FiraSans}
\usepackage[LGR,T1]{fontenc}
\usepackage[utf8]{inputenx}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepackage{showframe}
\newlength{\iconcircleradius}
\setlength{\iconcircleradius}{15pt}
\newlength{\iconcirclelinewidth}
\setlength{\iconcirclelinewidth}{1.5pt}
\newlength{\iconlabelsep}
\setlength{\iconlabelsep}{0.1cm}
\newlength{\headermatrixsep}
\setlength{\headermatrixsep}{\textwidth}
\newlength{\testlen}
\setlength{\testlen}{0pt}
\def\textA{Text piece A}
\def\textB{Text piece B}
\def\textC{Text piece C}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
% calculate separation lengths for icon-text elements such that they
% are evenly distributed across the \textwidth
% discard the nodes required for the calculations afterwards
\pgfdeclarelayer{discard}
\begin{pgfonlayer}{discard}
\foreach \infoelement in {\textA, \textB, \textC}
{%
\node[circle, line width=\iconcirclelinewidth, anchor=west, minimum height=2\iconcircleradius] (icon circle) at (0,0) {};
\node[font=\small, right=\iconlabelsep of icon circle, anchor=west] (text node) {\infoelement};
\newdimen\xEast
\pgfextractx{\xEast}{\pgfpointanchor{text node}{east}}
\newdimen\xWest
\pgfextractx{\xWest}{\pgfpointanchor{icon circle}{west}}
\global\advance\headermatrixsep by \dimexpr(- \xEast + \xWest + 2\iconcirclelinewidth)\relax
% introduce a test length to check if it accumulates to `\textwidth`
\global\advance\testlen by \dimexpr(\xEast - \xWest - 2\iconcirclelinewidth)\relax
}
\end{pgfonlayer}
\pgfmathsetlength{\headermatrixsep}{\headermatrixsep / (3*\fontscalingfac)}
\pgfmathsetlength{\testlen}{\testlen + (3*\fontscalingfac)*\headermatrixsep}
%
% place icon-text elements evenly across text width
% Icon A
\node[draw, circle, line width=\iconcirclelinewidth, anchor=west, minimum height=2\iconcircleradius] (icon A) at (current page text area.north west) {};
\node[color=black, font=\small, right=\iconlabelsep of icon A, anchor=west] (text A) {\textA};
% Icon B
\node[draw, circle, line width=\iconcirclelinewidth, minimum height=2\iconcircleradius, right=\headermatrixsep of text A, anchor=west] (icon B) {};
\node[color=black, font=\small, right=\iconlabelsep of icon B, anchor=west] (text B) {\textB};
% Icon C
\node[draw, circle, line width=\iconcirclelinewidth, minimum height=2\iconcircleradius, right=\headermatrixsep of text B, anchor=west] (icon C) {};
\node[color=black, font=\small, right=\iconlabelsep of icon C, anchor=west] (text C) {\textC};
\draw[red] ($(current page text area.north west) - (0, 10pt)$) -- ($(current page text area.north west) + (\testlen, -10pt)$);
\end{document}
This leads to the following picture
The variable \fontscalingfac
was introduced because in my real world example its introduction just about fixes the spacing issue (I scale the font in order for its xheight to match with the newtxsf
packages I use it with). In my MWE scaling the font actually leads to a slightly different spacing which I would also like to understand why. But my main issue is: Why does my method for distributing my drawing elements evenly across the page and thereby filling the whole width fail?
\tikz{...}\hfill\tikz{...}\hfill\tikz{...}
? One thing that prevents me from spelling this out is\usepackage[sfdefault,scaled=\fontscalingfac]{FiraSans}
which I never got to work on my machine. – user121799 May 8 '19 at 21:04scrlayer-scrpage
using\DeclareNewLayer
. I will have to think about it some more but I fear your approach might not be feasible in my situation. – Philipp May 8 '19 at 21:15\hfill
inside TikZ pictures IMHO. (If you want to see some basic attempts, have a quick look e.g. at this post, but I would say there is no general solution.) If you feel that what you achieved may help others there is nothing wrong with keeping the question and writing your own answer IMHO. – user121799 May 8 '19 at 22:21