0

I'm using tikz matrix to put some data on a grid. However depending on the height of what's in each node the edge linking two nodes are slightly slanted making an ugly effect of wavy line. How can I have those line straight ?

Here is an example of code generating the effect

\documentclass[a4paper]{amsart}

\usepackage{amsmath, amsfonts, amssymb, amsthm, wasysym}

\usepackage{pgfkeys} % tikz environnement \usepackage{tikz} \usetikzlibrary{trees, snakes, shapes, arrows, arrows.meta, matrix, calc, graphs, plotmarks, backgrounds, decorations.markings}

\begin{document} [ \begin{tikzpicture}[ultra thick, column sep={1.5cm,between origins}, row sep={1.0cm,between origins}] \matrix (M) [matrix of math nodes, nodes={inner sep=1pt}] { U_1 & \cdots & U_{i-1} & U_{i-1} \cup {i} & U_{i-1}^\sharp & U_{i}^\sharp & \cdots & U_{N} \ }; \foreach \n in {1,2,...,7} { \draw[thin] (M-1-\n.east) -- (M-1-\the\numexpr \n + 1\relax.west); } \end{tikzpicture} ] \end{document}

And here is what I get: Wavy row

I tried to fix the height of the node but it doesn't work. I also tried a trick such as tikz matrix arrow from empty nodes is not horizontal should do the work, but if possible, I would like to avoid inserting by hand some phantom text in front of each node (moreover such a text add some horizontal spacing).

hivert
  • 123
  • 1
    A matrix of nodes fixes anchor nodes to base, you have to hange them to center: nodes={inner sep=1pt, anchor=center} – Ignasi Aug 08 '23 at 15:47
  • Just a \mathstrut would probably be enough. And you can use execute at begin node=\mathstrut to let TikZ do it automatically for you. For more complex nodes you can use node families so that TikZ chooses the right maximum height and depth for all nodes. Though, the best solution would be to use the asymmetric rectangle shape from TikZ-CD. This is exactly why the shape exists. (It sets the west/center/east anchor a fixed but variable distance from the baseline.) – Qrrbrbirlbel Aug 08 '23 at 16:01
  • @ignasi This totally works ! Thank you. – hivert Aug 08 '23 at 18:07
  • You can also just do ([yshift=.5ex]M-1-\n.base east) -- ([yshift=.5ex]M-1-\the\numexpr \n + 1\relax.base west). – Qrrbrbirlbel Aug 09 '23 at 12:12

1 Answers1

2

Unless you need to draw the border of the nodes or fill them you can use the asymmetrical rectangle shape from TikZ-CD. This shape is made exactly for this reason.

In fact, the same diagram can be made just as easy with TikZ-CD.

The shape is set up so that the west/center/east anchor is a fixed but variable distance above the baseline. This distance is controlled the value of /tikz/commutative diagrams/center yshift which defaults to axis_height, a PGFMath function that returns

the axis height parameter (a.k.a. σ₂₂) of the document's math font.

Or in layman's terms: Where we want it for typesetting math content.

Code

\documentclass[varwidth]{standalone}
%\documentclass[a4paper]{amsart}
\usepackage{amsmath, amsfonts, amssymb, amsthm, wasysym}
% tikz environnement
\usepackage{tikz}
\usetikzlibrary{cd, matrix}

\begin{document} [ \begin{tikzpicture}[ ultra thick, column sep={1.5cm,between origins}, row sep={1.0cm,between origins}] \matrix (M) [ matrix of math nodes, nodes={inner sep=1pt, asymmetrical rectangle}, ]{ U_1 & \cdots & U_{i-1} & U_{i-1} \cup {i} & U_{i-1}^\sharp & U_{i}^\sharp & \cdots & U_{N} \ }; \foreach \n in {1,2,...,7} \draw[thin] (M-1-\n.east) -- (M-1-\the\numexpr \n + 1\relax.west); \end{tikzpicture} ] [ \begin{tikzcd}[ arrows=dash, cells={nodes={inner sep=1pt}}, column sep={1.5cm,between origins}, row sep={1cm,between origins}] U_1 \rar & \cdots \rar & U_{i-1} \rar & U_{i-1} \cup {i} \rar & U_{i-1}^\sharp \rar & U_{i}^\sharp \rar & \cdots \rar & U_{N} \end{tikzcd} ] \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821