Drawing a Rectangular Diagram with Tikz - Dan Seeks Guidance

  • Context:
  • Thread starter Thread starter topsquark
  • Start date Start date
  • Tags Tags
    Diagram Rectangular
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 5K views
Messages
2,020
Reaction score
843
I thought I'd try out the tikz thing.

I am trying to draw a square with the four points A, B, C, and D labeled along with functions listed by each "arrow." I based my code on the triangle that was on the tikz example. Something is obviously wrong.

Heck I'd just be happy to get the square to show.
Code:
\begin{tikzpicture}[blue]
  \coordinate (A) at (0,0);
  \coordinate (B) at (4,0);
  \coordinate (D) at (4,4);
  \coordinate (C) at (0,4);

  \draw[blue, ultra thick] (A) -- (B) -- (D) -- (C) cycle;
  \path (A) node[below left] {A} -- (B) node[below right] {B} -- (D) node[above right] {D} -- (C) node[above left];
  \path (A) -- node[below] {f} (B) -- node[right] {$\beta$} (D) -- node[above left] {g} (C) -- node[left] {$\alpha$} (A);
\end{tikzpicture}

Would someone please guide me through this one? And how would you make the lines arrows?

Thanks!

-Dan
 
on Phys.org
Here's a starting point (I guess). Just hacked it!

\begin{tikzpicture}[blue]
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\coordinate (D) at (4,4);
\coordinate (C) at (0,4);

\draw[blue, ultra thick] (A) -- (B);
\draw[blue, ultra thick] (B) -- (D);
\draw[blue, ultra thick] (D) -- (C);
\draw[blue, ultra thick] (C) -- (A);
\path (A) node[below left]{A};
\path (B) node[below right]{B};
\path (D) node[above right]{C};
\path (C) node[above left]{D};
\path (A) -- node[below] {f} (B) -- node
{$\beta$} (D) -- node[above left] {g} (C) -- node
{$\alpha$} (A);
\end{tikzpicture}​
 
Hi Dan,

I see greg1313 already fixed your immediate issues for you and got the square to show. Good!
Anyway, let me try to guide you through the process.
For starters I recommend to use the Live TikZ Editor that is the topmost item in the MHB Widgets that is visible on the right when responding.

  1. When I paste the code into the editor, or if I simply paste it here, I get:
    \begin{tikzpicture}[blue]
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    \coordinate (D) at (4,4);
    \coordinate (C) at (0,4);

    \draw[blue, ultra thick] (A) -- (B) -- (D) -- (C) cycle;
    \path (A) node[below left] {A} -- (B) node[below right] {B} -- (D) node[above right] {D} -- (C) node[above left];
    \path (A) -- node[below] {f} (B) -- node
    {$\beta$} (D) -- node[above left] {g} (C) -- node
    {$\alpha$} (A);
    \end{tikzpicture}
    Apparently TikZ doesn like the reference to [m]cycle[/m] in [M]\draw[blue, ultra thick] (A) -- (B) -- (D) -- (C) cycle;[/M].
    And indeed it should be [M]\draw[blue, ultra thick] (A) -- (B) -- (D) -- (C) -- cycle;[/M].
    After all, the cycle keyword is supposed to complete a polygon of points that are each connected with [M]--[/M].[*]After fixing that, I get:
    \begin{tikzpicture}[blue]
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    \coordinate (D) at (4,4);
    \coordinate (C) at (0,4);

    \draw[blue, ultra thick] (A) -- (B) -- (D) -- (C) -- cycle;
    \path (A) node[below left] {A} -- (B) node[below right] {B} -- (D) node[above right] {D} -- (C) node[above left];
    \path (A) -- node[below] {f} (B) -- node
    {$\beta$} (D) -- node[above left] {g} (C) -- node
    {$\alpha$} (A);
    \end{tikzpicture}
    Now TikZ doesn't like the [M]-- (C) node[above left];[/M].
    A node must indeed have a label text, which is denoted by [M]{label text}[/M].
    So it should be [M]-- (C) node[above left] {C};[/M].[*]After fixing that, we get what greg1313 already posted.


To get arrows, we need to add the attribute [M][->][/M].
That is, use [M]\draw[->] (A) -- (B);[/M].
However, the one draw command we have, draws a closed polygon, so there is no room for an arrow there.
After splitting it into:
Code:
  \draw[->] (A) -- (B);
  \draw[->] (C) -- (D);
  \draw[->] (A) -- (C);
  \draw[->] (B) -- (D);
we get:
\begin{tikzpicture}[blue,ultra thick]
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\coordinate (D) at (4,4);
\coordinate (C) at (0,4);

\draw[->] (A) -- (B);
\draw[->] (C) -- (D);
\draw[->] (A) -- (C);
\draw[->] (B) -- (D);
\path (A) node[below left] {A} -- (B) node[below right] {B} -- (D) node[above right] {D} -- (C) node[above left] {C};
\path (A) -- node[below] {f} (B) -- node
{$\beta$} (D) -- node[above left] {g} (C) -- node
{$\alpha$} (A);
\end{tikzpicture}​
 
Is this what you seek?$$\begin{array}
AA & \stackrel{f}{\longrightarrow} & C \\
\downarrow{\alpha} & & \downarrow{\beta} \\
B & \stackrel{g}{\longrightarrow} & D
\end{array}$$
Use the right mouse button to see the TeX code.
 
We can improve the drawing a bit.
  1. Use nodes for the points instead of coordinates, so that some white space is left around the points.
    That is, use [M]\node (A) at (0,0) {A}[/M] instead of [M]\coordinate (A) at (0,0)[/M].
  2. Use the [M][>=stealth][/M] attribute to specify that arrowheads should be stealthy.
  3. Just for fun, add another arrow that bends a bit with [M]edge[bend left][/M].
  4. Leave out the blue and ultrathick, since they don't help us.
  5. Keep the labels of the arrows together with the arrows to keep the administration in one place.
Result is:
\begin{tikzpicture}[>=stealth]
\node (A) at (0,0) {A};
\node (B) at (4,0) {B};
\node (D) at (4,4) {D};
\node (C) at (0,4) {C};
\draw[->] (A) -- node[below] {f} (B);
\draw[->] (C) -- node[above] {g} (D);
\draw[->] (A) -- node
{$\alpha$} (C);
\draw[->] (B) -- node
{$\beta$} (D);
\draw[->] (A) edge[bend left] node[below right] {$g\circ\alpha$} (D);
\end{tikzpicture}
[latexs]\begin{tikzpicture}[>=stealth]
\node (A) at (0,0) {A};
\node (B) at (4,0) {B};
\node (D) at (4,4) {D};
\node (C) at (0,4) {C};
\draw[->] (A) -- node[below] {f} (B);
\draw[->] (C) -- node[above] {g} (D);
\draw[->] (A) -- node
{$\alpha$} (C);
\draw[->] (B) -- node
{$\beta$} (D);
\draw[->] (A) edge[bend left] node[below right] {$g\circ\alpha$} (D);
\end{tikzpicture}[/latexs]​
 
I like Serena said:
We can improve the drawing a bit.
  1. Use nodes for the points instead of coordinates, so that some white space is left around the points.
    That is, use [M]\node (A) at (0,0) {A}[/M] instead of [M]\coordinate (A) at (0,0)[/M].
  2. Use the [M][>=stealth][/M] attribute to specify that arrowheads should be stealthy.
  3. Just for fun, add another arrow that bends a bit with [M]edge[bend left][/M].
  4. Leave out the blue and ultrathick, since they don't help us.
  5. Keep the labels of the arrows together with the arrows to keep the administration in one place.
Result is:
\begin{tikzpicture}[>=stealth]
\node (A) at (0,0) {A};
\node (B) at (4,0) {B};
\node (D) at (4,4) {D};
\node (C) at (0,4) {C};
\draw[->] (A) -- node[below] {f} (B);
\draw[->] (C) -- node[above] {g} (D);
\draw[->] (A) -- node
{$\alpha$} (C);
\draw[->] (B) -- node
{$\beta$} (D);
\draw[->] (A) edge[bend left] node[below right] {$g\circ\alpha$} (D);
\end{tikzpicture}
[latexs]\begin{tikzpicture}[>=stealth]
\node (A) at (0,0) {A};
\node (B) at (4,0) {B};
\node (D) at (4,4) {D};
\node (C) at (0,4) {C};
\draw[->] (A) -- node[below] {f} (B);
\draw[->] (C) -- node[above] {g} (D);
\draw[->] (A) -- node
{$\alpha$} (C);
\draw[->] (B) -- node
{$\beta$} (D);
\draw[->] (A) edge[bend left] node[below right] {$g\circ\alpha$} (D);
\end{tikzpicture}[/latexs]​

Show off. (Muscle)

Excellent. And I really like the bent line. Nice touch.

Thanks!

-Dan​
 
steenis said:
Is this what you seek?$$\begin{array}
AA & \stackrel{f}{\longrightarrow} & C \\
\downarrow{\alpha} & & \downarrow{\beta} \\
B & \stackrel{g}{\longrightarrow} & D
\end{array}$$
Use the right mouse button to see the TeX code.
Your coding was what I was going to do at first but I thought I'd play around the the tikz as I don't know much about it.

Thanks for the response. (Handshake)

-Dan
 
Here's yet another way to make a commutative diagram using the dedicated [M]cd[/M] tikzlibrary.
\begin{tikzpicture}
\usetikzlibrary{cd}
\node {
\begin{tikzcd}
A \arrow{r}{f} \arrow{d}{\alpha} & B \arrow{d}{\beta} \\
C \arrow{r}{g} & D
\end{tikzcd}
};
\end{tikzpicture}
[latexs]\begin{tikzpicture}
\usetikzlibrary{cd}
\node {
\begin{tikzcd}
A \arrow{r}{f} \arrow{d}{\alpha} & B \arrow{d}{\beta} \\
C \arrow{r}{g} & D
\end{tikzcd}
};
\end{tikzpicture}[/latexs]
Note that we don't need any dollars (\$) to wrap math symbols.
That's because the whole tikzcd environment is in math mode.
It's really optimized for commutative diagrams.

Unfortunately at present we have to wrap the tikzcd diagram in a [M]\node[/M], because otherwise we're losing half of it.
That's because the tikzcd environment includes the tikzpicture environment, and TikZ doesn't like such a recursive inclusion.
Either way, it works fine if we wrap it in a [M]\node[/M]. And I'm still looking for a better way to support tikzcd diagrams without such a work-around.