Drawing a Rectangular Diagram with Tikz - Dan Seeks Guidance

  • Context: MHB 
  • Thread starter Thread starter topsquark
  • Start date Start date
  • Tags Tags
    Diagram Rectangular
Click For Summary

Discussion Overview

The discussion revolves around using TikZ to create a rectangular diagram with labeled points and arrows. Participants share code snippets, troubleshoot issues, and suggest improvements for drawing techniques and aesthetics.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Dan expresses difficulty in drawing a square with labeled points and seeks guidance on using TikZ.
  • Another participant provides a modified version of Dan's code that successfully displays the square, indicating a correction in the use of the "cycle" keyword.
  • A participant suggests using the Live TikZ Editor for real-time code testing and highlights issues with node labeling in the original code.
  • There are suggestions to add arrows to the lines, with a discussion on how to implement this correctly in TikZ.
  • Dan acknowledges the help and expresses satisfaction with the improvements made to the diagram.
  • Another participant proposes using nodes instead of coordinates to improve spacing and aesthetics, along with the addition of a bent arrow for visual interest.
  • Multiple participants share variations of the diagram, including a commutative diagram using a dedicated TikZ library.

Areas of Agreement / Disagreement

Participants generally agree on the corrections and improvements suggested for the TikZ code, but there are multiple approaches presented for achieving the desired diagram, indicating a lack of consensus on a single best method.

Contextual Notes

Some participants mention specific attributes and libraries in TikZ that can enhance the diagram's appearance, but there are unresolved questions about the best practices for labeling and arrow styles.

Who May Find This Useful

This discussion may be useful for individuals interested in learning how to use TikZ for creating diagrams, particularly in mathematical or physics contexts, as well as those looking for troubleshooting tips and aesthetic enhancements in their diagrams.

topsquark
Science Advisor
Homework Helper
Insights Author
MHB
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
 
Physics news 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}​
 
Beautiful. Thanks to you both.

-Dan
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 0 ·
Replies
0
Views
6K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 27 ·
Replies
27
Views
4K
  • Poll Poll
  • · Replies 1 ·
Replies
1
Views
2K