2

I have a command \entry which places two tikz-nodes next to each other. How can I make sure both nodes always have the same height (while also keeping the vertical centering of the text inside)?

Also, how come the right node extends the \linewidth, if both nodes together (0.25\linewidth+0.75\linewidth) should be equal to exactly one \linewidth? I would expect the total width of both nodes to be equal to the text width of the bottom text. How would I fix that?

enter image description here

MWE:

\documentclass[
    12pt,
    parskip=half,
]{scrartcl}

\usepackage[ a4paper, left=25mm, right=20mm, top=25mm, bottom=30mm, showframe, ]{geometry} \usepackage{blindtext}

% Font \usepackage{inconsolata} \usepackage[T1]{fontenc} \renewcommand*\familydefault{\sfdefault} %% Only if the base font of the document is to be sans serif

\usepackage{tikz} \usetikzlibrary{positioning}

\definecolor{colour-a}{HTML}{eefaea} \definecolor{colour-b}{HTML}{e8f4f2}

\newcommand\entry[2]{% \begin{tikzpicture} \node(node-a)[fill=colour-a, text width=0.25\linewidth] { \textbf{#1} }; \node(node-b)[fill=colour-b, right=0pt of node-a, align=left, text width=0.75\linewidth]{ #2 }; \draw[thick] (node-b.south west)--(node-b.north west); \end{tikzpicture} \par % end of previous paragraph %\vspace*{1ex}% optional vertical space in between }

\begin{document} \entry{% test1 }{ test1 }

\entry{%
    test2 \\
    test2 \\
    test2 \\
}{
    test2
}

\entry{%
    test3
}{
    test3 \\
    test3 \\
    test3 \\
}

\entry{%
    test4
}{
    \footnotesize \blindtext
}

\textbf{Bottom Text}: \\
\blindtext

\end{document}

Hanns
  • 33

3 Answers3

2

Your question was answered by Zarko. It is necessary to add twice the inner spacing to the width of the text to obtain the node whose full width will be, e.g. 0.25\textwidth.

But it's worth considering that using a tabular(x) with a column X will simplify the code as the computation to fit to \linewidth will be handled internally.

nicematrix (uses tikz internally) allows the command Block to center vertically and color the cell, and also use \\ inside.

As a bonus, notice that the vertical lines take on the length of the tallest cell.

a

\documentclass[
12pt,
parskip=half,
]{scrartcl}

\usepackage[ a4paper, left=25mm, right=20mm, top=25mm, bottom=30mm, ]{geometry} \usepackage{blindtext}

% Font \usepackage{inconsolata} \usepackage[T1]{fontenc} \renewcommand*\familydefault{\sfdefault} %% Only if the base font of the document is to be sans serif

%**************************************** added <<<<<<<<<<<<<<<<<<<<< \usepackage{ragged2e} %\justifying \usepackage{showframe}

\usepackage{nicematrix} \definecolor{colour-a}{HTML}{eefaea} \definecolor{colour-b}{HTML}{e8f4f2} \newcolumntype{P}[1]{m{#1}}

\newcommand\dateentry[2]{% ********* changed <<<<<<<<<<<< \begin{NiceTabularX}{\textwidth}{P{0.25\textwidth}| X}
\Block[l, fill= colour-a ]{1-1}{#1}&\Block[fill= colour-a]{1-1}{\justifying#2} \ \end{NiceTabularX} \par% end of previous paragraph } %*********************************************************

\begin{document}

\dateentry{test1}{test1}
\dateentry{test1 \\ test2 \\ test2}{test2}
\dateentry{test3}{ test3 \\ test3 \\ test3}
\dateentry{test4}{ \footnotesize \blindtext }   

\textbf{Bottom Text}: \newline
\blindtext \end{document}

Simon Dispa
  • 39,141
1

This is a proposal with sidebyside tcolorboxes instead of TikZ nodes. In this case the total width is \linewidth by deafult without no worries.

sidebyside boxes are unbreakable, but as TiKZ nodes are also unbreakable, this shouldn't be a problem.

\documentclass[12pt,parskip=half]{scrartcl}
\usepackage[
    a4paper,
    hmargin={25mm,20mm}, vmargin={25mm,30mm},
    showframe,
            ]{geometry}
\usepackage{blindtext}
% Font
\usepackage{inconsolata}
\usepackage[T1]{fontenc}
\renewcommand*\familydefault{\sfdefault} % Only if the base font is sans serif

\usepackage[most]{tcolorbox}

\newtcolorbox{mysidebysidebox}[1][]{ sidebyside, sidebyside align=center seam, lefthand width=.25\linewidth, sharp corners, bicolor, overlay={\draw[black, very thick] (segmentation.north)--(segmentation.south);}, frame hidden, colback=colour-a, colbacklower=colour-b, #1 }

\newcommand{\entry}[3][]{% \begin{mysidebysidebox}[#1] #2\tcblower #3 \end{mysidebysidebox} }

\definecolor{colour-a}{HTML}{eefaea} \definecolor{colour-b}{HTML}{e8f4f2}

\begin{document} \entry{% test1 }{ test1 }

\entry{%
    test2 \\
    test2 \\
    test2 \\
}{
    test2
}

\entry{%
    test3
}{
    test3 \\
    test3 \\
    test3 \\
}

\entry[colbacklower=blue!20]{%
    test4
}{
    \footnotesize \blindtext
}

\textbf{Bottom Text}: \\
\blindtext

\end{document}

enter image description here

Ignasi
  • 136,588
0

Edit:
Apparently I read the title of the question too superficially:(.

  • Your conclusion wrong. See @Qrrbrbirlbel comment. Possible solution is to define text width for example as
text width=0.25\linewidth-2*\pgfkeysvalueof{/pgf/inner xsep} 
  • A (relatively) simple way that both nodes will have the same height, is replacing them with one multi part node
\documentclass[12pt,parskip=half]{scrartcl}
\usepackage[
    a4paper,
    hmargin={25mm,20mm}, vmargin={25mm,30mm},
    showframe,
            ]{geometry}
\usepackage{blindtext}
% Font
\usepackage{inconsolata}
\usepackage[T1]{fontenc}
\renewcommand*\familydefault{\sfdefault}

\usepackage{tikz} \usetikzlibrary{shapes.multipart}

\definecolor{colour-a}{HTML}{eefaea} \definecolor{colour-b}{HTML}{e8f4f2}

\newcommand\entry[2]% { \begin{tikzpicture}[ mpnh/.style = { rectangle split, rectangle split horizontal, rectangle split parts=2, rectangle split part fill={colour-a,colour-b}, } ] \node (a) [mpnh] { \nodepart[text width=0.25\linewidth-2\pgfkeysvalueof{/pgf/inner xsep}]{one} \textbf{#1} \nodepart[text width=0.75\linewidth-2\pgfkeysvalueof{/pgf/inner xsep}]{two} #2 }; \draw[thick] (a.one split north)--(a.one split south); \end{tikzpicture}\par }

\begin{document} \entry{test 1}{test 1} \entry{test 2a\ test 2b\ test 2c}{test 2} \entry{test 3}{test 3a \ test 3b \ test 3c} \entry{test 4}{\blindtext}

\textbf{Bottom Text}: \ \blindtext \end{document}

enter image description here

Another way is use table instead of draw node. With use of the tabularray package code is much simpler and shorter:

\documentclass[12pt,parskip=half]{scrartcl}
\usepackage[a4paper,
            hmargin={25mm,20mm}, vmargin={25mm,30mm},
            showframe,
            ]{geometry}
\usepackage{blindtext}
% Font
\usepackage[T1]{fontenc}
\usepackage{inconsolata}
\usepackage{microtype}
\renewcommand*\familydefault{\sfdefault}

\usepackage{xcolor} \usepackage{tabularray} \makeatletter \ExplSyntaxOn \pretocmd{@declaredcolor}{\my@hack@color{#1}}{}{} \def\my@hack@color#1{@ifundefined{@backslashchar color@#1}{\MyDefineColor{#1}}{\relax}} \cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }

\NewDocumentCommand \MyDefineColor { m } { \seq_set_split:Nnx \l_tmpa_seq {|} {#1} %\seq_show:N \l_tmpa_seq \exp_args:Nnxx \definecolor{#1} { \seq_item:Nn \l_tmpa_seq {1} } { \seq_item:Nn \l_tmpa_seq {2} } } \ExplSyntaxOff \makeatother

\definecolor{colour-a}{HTML}{eefaea} \definecolor{colour-b}{HTML}{e8f4f2} \newcommand\entry[2]% { \begin{tblr}{colspec = {X[1, l, bg={HTML|eefaea}] | X[3, j,m, bg={HTML|e8f4f2}]}} \textbf{#1} & #2
\end{tblr}\par }

\begin{document} \entry{test 1}{test 1} \entry{test 2a\ test 2b\ test 2c}{test 2} \entry{test 3}{test 3a \ test 3b \ test 3c} \entry{test 4}{\small \blindtext}

\textbf{Bottom Text}: \ \blindtext \end{document}

enter image description here

Zarko
  • 296,517