Drawing a Rectangular Diagram with Tikz - Dan Seeks Guidance

In summary, to draw a square with labeled points and functions listed by arrows, one can use the tikzpicture environment. The square can be drawn with the coordinates A, B, C, and D using the \draw command and the cycle keyword. The labels of the points can be added using the \path command. To make the lines arrows, one can add the [->] attribute to the \draw command. For a more visually appealing drawing, the points can be replaced with nodes and the [>=stealth] attribute can be added to specify stealthy arrowheads. Additional arrows can be added using the \draw command with the edge[bend left] attribute.
  • #1
topsquark
Science Advisor
Insights Author
Gold Member
MHB
2,020
827
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
  • #2
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}​
 
  • #3
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}​
 
  • #5
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.
 
  • #6
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]​
 
  • #7
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​
 
  • #8
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
 
  • #9
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.
 

FAQ: Drawing a Rectangular Diagram with Tikz - Dan Seeks Guidance

What is Tikz and why is it used for drawing rectangular diagrams?

Tikz is a powerful and versatile tool for creating high-quality graphics in LaTeX. It is often used for drawing diagrams, graphs, and other visual representations in scientific documents, as it offers a wide range of customization options and produces professional-looking results.

How do I get started with creating a rectangular diagram using Tikz?

The best way to get started is to familiarize yourself with the basic syntax of Tikz and its various commands. This can be done by reading the Tikz documentation or following online tutorials. It's also helpful to have a basic understanding of LaTeX, as Tikz graphics are typically integrated into LaTeX documents.

Are there any specific packages or libraries I need to use for drawing rectangular diagrams with Tikz?

Yes, there are several packages and libraries that are commonly used with Tikz for drawing diagrams. Some of the most popular ones include tikzpicture, tikz-uml, and tikz-network. These packages offer additional tools and features that make it easier to create specific types of diagrams.

Can I customize the appearance of my rectangular diagram using Tikz?

Absolutely! Tikz offers a wide range of customization options, such as changing the color, size, and style of your diagram elements. You can also add labels, annotations, and other details to make your diagram more informative and visually appealing.

Are there any limitations to using Tikz for drawing rectangular diagrams?

Tikz is a powerful tool, but like any other software, it does have some limitations. One of the main limitations is that it can be time-consuming to learn and master, especially for beginners. Additionally, complex diagrams with a large number of elements may require advanced coding skills and may take longer to create. However, with practice and patience, Tikz can be a valuable tool for creating professional-looking rectangular diagrams.

Similar threads

Replies
5
Views
2K
Replies
1
Views
1K
Replies
1
Views
5K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
3
Views
4K
Replies
8
Views
2K
Replies
1
Views
2K
Back
Top