2

Code:

\[
\begin{tikzcd}[cells={nodes={draw=gray}}]
    \text{Math is excellent!} \ar[r, rightarrow] \ar[d, leftarrow]
        & \text{w} \ar[d, rightarrow]\\
    \text{w} \ar[u, rightarrow] \ar[r, leftarrow]
        & \text{Pythagoreans killed someone!}
\end{tikzcd}
\]

Output:

enter image description here

Requirement: I am trying to achieve something like this:

enter image description here

  1. I know that Case 1 is not possible since the columns (and rows) are formed by centering the cells.

  2. So I try to implement Case 2 which does seem achievable. In particular, I am trying to do this:

    • Make the width of the cells in a column equal.
    • Then somehow write on that cell at a specific place. (Left aligned on the top right, and right aligned on the bottom left.)

Note: I deliberately write "at a specific place", and not just the special cases of left and right alignments.

Can this be done? (I thought of using \phantom. But that obviously gives crude results in this case.)

I know that how to modify the arrow anchor points so it is not a problem.

3
  • Is tikz-cd mandatory for your future use? If not, I think that a simple TikZ graph would be a better solution.
    – SebGlav
    Apr 2 at 22:46
  • I am trying to avoid TikZ since it seems overwhelmingly complicated to me than the friendly and simple tikz-cd.
    – Atom
    Apr 2 at 22:50
  • I can hear that, but if you're not to draw more complicated graph than this one, it's pretty straightforward in plain TikZ and would be a pain to achieve in tikz-cd. Hope someone will find a solution for you.
    – SebGlav
    Apr 2 at 22:52

2 Answers 2

4

Just to let you know how to do this in plain TikZ, even if this is not what you're asking for:

four nodes and arrows

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}

\begin{document}

    \begin{tikzpicture}[
            node distance = 2cm and 5cm,
            every node/.style = {
                rectangle,
                draw,
                inner sep = 5pt,
                minimum size = 1cm},
            > = {Straight Barb}
            ]
        \node[left] (A) {Math is excellent!};
        \node[right, right= of A] (B) {w};
        \node[below = of A.east, anchor = east] (C) {w};
        \node[below = of B.west, anchor = west] (D) {Pythagoreans kill someone!};
        
        \path[->]
            (A) edge (B)
            (B.south) edge (D.north-|B)
            (D) edge (C)
            (C.north) edge (A.south-|C);
    \end{tikzpicture}

\end{document}

There are better ways to do this (but more complex), so I just wrote a simple code.

1
  • I guess it's my time to try my hands on TikZ.
    – Atom
    Apr 2 at 23:10
4

Here is a tikz-cd solution. The idea is to use \fcolorbox to draw the boxes around the text, and \llap and \rlap to control the arrow placement. Box color is controlled globally with \cdboxcolor, set to gray.

Define new commands \leftbox, \rightbox and \cdbox:

\leftbox{<dim>}{<text>} places <text> in a box that is shifted left so the tikzcd arrow is aimed as if the box had width <dim> instead of its actual width. \rightbox is defined analogously. \cdbox{<text>} does not shift.

enter image description here

\documentclass{article}

\usepackage{tikz-cd,amsmath}

\newcommand{\cdboxcolor}{gray}
\newcommand{\leftbox}[2]{\hspace{#1}\llap{\fcolorbox{\cdboxcolor}{white}{#2}}}
\newcommand{\rightbox}[2]{\rlap{\fcolorbox{\cdboxcolor}{white}{#2}}\hspace{#1}}
\newcommand{\cdbox}[1]{\fcolorbox{\cdboxcolor}{white}{#1}}

\begin{document}

\[
\begin{tikzcd}[cells={nodes={inner sep=0pt}}]
    \leftbox{1cm}{Math is excellent!} \ar[r, rightarrow] \ar[d, leftarrow]
        & \cdbox{w} \ar[d, rightarrow]\\
    \cdbox{w} \ar[u, rightarrow] \ar[r, leftarrow]
        & \rightbox{1cm}{Pythagoreans killed someone!}
\end{tikzcd}
\]

\end{document}
2
  • Interesting idea, but is there any way to adjust the boxes anchors in order to align them to the left or to the right like we do with TikZ?
    – SebGlav
    Apr 3 at 9:53
  • 1
    @SebGlav: I couldn't think of an elementary way of doing this. Trying to adjust the anchors in tikz-cd seems much more complicated than learning how to do this with TikZ, as you suggested. :-)
    – Sandy G
    Apr 3 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.