8

Before I ask this question, I have clicked many pages. Unluckily, I have found none. Could someone tell me how to draw the simple geometric figure with arrow via tex?

Be grateful so much!

enter image description here

Hinn
  • 765

2 Answers2

13

A strongly recommended tikz solution ;-)

\documentclass[tikz,border=12pt]{standalone}
\usetikzlibrary{arrows.new}
\tikzset{every node/.append style = {pos=.5,fill=white,font = \footnotesize,outer sep =0pt, inner sep = 1pt}}
\begin{document}
\begin{tikzpicture}[thick]
    \draw[help lines,densely dotted,thin] (0,0) grid (8,4);
    \draw (1,1) -- (3,1) -- (2,3) -- (1,3) -- cycle;
    \draw[|<->|] (1,3.2) -- (2,3.2) node {1};
    \draw[|<->|] (.8,1) -- (.8,3) node {2};
    \draw[|<->|] (1,.8) -- (3,.8) node {2};
    %% second picture
    \draw (5,1) rectangle (7,3);
    \draw (5,2) -- (6,2) -- (6,3) (6,2) -- (7,1); 
    \draw[|<->|] (4.8,1) -- (4.8,2) node {1};
    \draw[|<->|] (4.8,2) -- (4.8,3) node {1};
\end{tikzpicture}
\end{document}

enter image description here

We put captions now:

  1. Using nodes

\documentclass[tikz,border=12pt]{standalone}
\usetikzlibrary{arrows.new}
\tikzset{mynode/.style = {pos=.5,fill=white,font = \footnotesize,outer sep =0pt, inner sep = 1pt}}
\usepackage{caption}
\begin{document}
\begin{tikzpicture}[thick]
    \draw[help lines,densely dotted,thin] (0,0) grid (8,4);
    \draw (1,1) -- (3,1) -- (2,3) -- (1,3) -- cycle;
    \draw[|<->|] (1,3.2) -- (2,3.2) node[mynode] {1};
    \draw[|<->|] (.8,1) -- (.8,3) node[mynode] {2};
    \draw[|<->|] (1,.8) -- (3,.8) node[mynode] {2};
    \node[text width=2cm,anchor=north,inner sep = 0pt,,] at (1.8,.9) {\captionof{figure}{figure 1}};     %% adjust .9 for vertical placement, This uses caption package and put Figure 1: automatically
    %% second picture
    \draw (5,1) rectangle (7,3);
    \draw (5,2) -- (6,2) -- (6,3) (6,2) -- (7,1);
    \draw[|<->|] (4.8,1) -- (4.8,2) node[mynode] {1};
    \draw[|<->|] (4.8,2) -- (4.8,3) node[mynode] {1};
    \node[text width=2cm,align=left,anchor=north,inner sep = 0pt] at (5.8,.6) {figure is here with a long caption};     %% adjust .6 for vertical placement
\end{tikzpicture}
\end{document}

enter image description here

  1. Using figure environment

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.new}
\tikzset{mynode/.style = {pos=.5,fill=white,font = \footnotesize,outer sep =0pt, inner sep = 1pt}}

\begin{document}
\begin{figure}[htb]
\begin{minipage}[t]{.5\textwidth}
\centering
\begin{tikzpicture}[thick]
    \draw[help lines,densely dotted,thin] (0,0) grid (4,4);
    \draw (1,1) -- (3,1) -- (2,3) -- (1,3) -- cycle;
    \draw[|<->|] (1,3.2) -- (2,3.2) node[mynode] {1};
    \draw[|<->|] (.8,1) -- (.8,3) node[mynode] {2};
    \draw[|<->|] (1,.8) -- (3,.8) node[mynode] {2};
\end{tikzpicture}
\caption{figure 1}
\end{minipage}%
\begin{minipage}[t]{.5\textwidth}
\centering
\begin{tikzpicture}[thick]
    %% second picture
    \draw[help lines,densely dotted,thin] (4,0) grid (8,4);
    \draw (5,1) rectangle (7,3);
    \draw (5,2) -- (6,2) -- (6,3) (6,2) -- (7,1);
    \draw[|<->|] (4.8,1) -- (4.8,2) node[mynode] {1};
    \draw[|<->|] (4.8,2) -- (4.8,3) node[mynode] {1};    
\end{tikzpicture}
\caption{figure is here with a long caption}
\end{minipage}
\end{figure}
\end{document}

enter image description here

  • You should not repeat |<->| as it will violate the DRY (Don't Repeat Yourself) principle. :-) – kiss my armpit Nov 13 '13 at 08:02
  • @Marienplatz I don't want to DRY up ;-). Some times DRY makes code crptic and may scare users. So I reserve DRY only for my real world documents. :-) –  Nov 13 '13 at 09:05
  • @HarishKumar Thanks for your reply. I have solved my problem. Would you mind one more question? How to add the corresponding captions at the bottom of the two figures? – Hinn Nov 13 '13 at 09:12
  • 1
    @Hinn: How would you like it? 1. adding a node or 2. using figure environment? –  Nov 13 '13 at 09:50
  • @HarishKumar I like both of them. Please see my question for it https://www.evernote.com/shard/s192/sh/1cb3ab4d-27bc-412d-9e47-f9cb7be0cfb3/f4f953765e76b1310fc3c9539cdc4244 Thanks! – Hinn Nov 13 '13 at 14:24
  • @Hinn Answer updated :-) –  Nov 13 '13 at 22:39
  • @HarishKumar You help me solve all of my problems. It's very kind of you! Thank you & Best wishes ^0^ – Hinn Nov 14 '13 at 01:27
  • excuse me, just one more question. Why the caption of figure 1 appears "Figure 1", but the second figure not? If I only want the type of second caption, how can I do that? Actually, when I use \begin{figure}, the caption always appears "Figure 1:***". Thanks! – Hinn Nov 23 '13 at 14:38
  • @Hinn First figure uses \captionof whereas second doesn't. :-) –  Nov 23 '13 at 14:42
  • @HarishKumar you're using node at the first case. However, if we use figure environment, how can we make "figure 1" disappeared? Now I'm considering to ask a question about this. – Hinn Nov 23 '13 at 15:32
  • @Hinn Put \usepackage[labelformat=empty]{caption} in the preamble to get rid of Figure 1 etc. –  Nov 23 '13 at 15:56
  • @HarishKumar It works perfectly. Many thanks! – Hinn Nov 24 '13 at 00:09
10

An extremely recommended solution with PSTricks, only for best-practitioners.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}
\psset{arrows=|<*->|*,shortput=nab,offset=-6pt}

\def\Left{%
    \pnodes{A}(1,1)(3,1)(2,3)(1,3)
    \pspolygon(A0)(A1)(A2)(A3)% I need \psnpolygon(0,3){A} but it does not exist! :-(
    \pcline(A0)(A1)_{$2$}
    \pcline(A3)(A0)\ncput*{$2$}
    \pcline(A2)(A3)_{$1$}}
\def\Right{%
    \pnodes{A}(1,1)(3,1)(3,3)(2,3)(1,3)(1,2)(2,2)
    \pspolygon(A0)(A1)(A2)(A4)
    \psline{-}(A5)(A6)(A3)
    \psline{-}(A1)(A6)
    \pcline(A4)(A5)\ncput*{$1$}
    \pcline(A5)(A0)\ncput*{$1$}}
\begin{document}
\begin{pspicture}[showgrid=true](8,4)
    \Left\rput(4,0){\Right}
\end{pspicture}
\end{document}

enter image description here

PDFLaTeX friendly

Compile it with pdflatex -shell-escape host where host.tex is the name of the following code. Based on your comment I added 3 captions, one for each figure and one for both figures as a group.

% name this file as host.tex
% and compile with
% pdflatex -shell-escape host
\documentclass[preview,border=12pt]{standalone}
\usepackage{filecontents}

\begin{filecontents*}{diagram.tex}
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}
\psset{arrows=|<*->|*,shortput=nab,offset=-6pt}

\def\Left{%
    \pnodes{A}(1,1)(3,1)(2,3)(1,3)
    \pspolygon(A0)(A1)(A2)(A3)% I need \psnpolygon(0,3){A} but it does not exist! :-(
    \pcline(A0)(A1)_{$2$}
    \pcline(A3)(A0)\ncput*{$2$}
    \pcline(A2)(A3)_{$1$}}
\def\Right{%
    \pnodes{A}(1,1)(3,1)(3,3)(2,3)(1,3)(1,2)(2,2)
    \pspolygon(A0)(A1)(A2)(A4)
    \psline{-}(A5)(A6)(A3)
    \psline{-}(A1)(A6)
    \pcline(A4)(A5)\ncput*{$1$}
    \pcline(A5)(A0)\ncput*{$1$}}
\begin{document}
\begin{pspicture}[showgrid=false](4,4)
    \Left
\end{pspicture}
\begin{pspicture}[showgrid=false](4,4)
    \Right
\end{pspicture}
\end{document}
\end{filecontents*}

\immediate\write18{latex diagram && dvips diagram && ps2pdf -dAutoRotatePages=/None diagram.ps}

\usepackage{graphicx,subcaption}

\begin{document}
PSTricks was born to make your life better.
\begin{figure}
\centering% you can also remove this line by changing the subfigure width.
\subfigure[b]{.4\linewidth}
\centering
\includegraphics[page=1]{diagram}
\caption{The left diagram}\label{fig:Left}
\endsubfigure
\subfigure[b]{.4\linewidth}
\centering
\includegraphics[page=2]{diagram}
\caption{The right diagram}\label{fig:Right}
\endsubfigure
\caption{Two nice diagrams side by side}\label{fig:Main}
\end{figure}
\end{document}

enter image description here

  • I'm sorry. It fails to compile in my PC. It says that"Undefined control sequence, \Left\rput(4,0){\Right}". Before compiling, shall I have to download a certain software? Actually I want to show the figure after the frame title via slideshow. Could you please adjust a little bit for that? Thanks a lot! – Hinn Nov 13 '13 at 07:42
  • @Hinn: PSTricks can't be compiled directly using pdflatex, you'll need some trickery for that. Harish's solution uses TikZ, which will compile with any compiler. – Jake Nov 13 '13 at 07:50
  • Not the animation, but the frame around the image. :-) –  Nov 13 '13 at 07:51
  • @HarishKumar Marienplatz Jake. Thank you first, please see https://www.evernote.com/shard/s192/sh/5c8110ab-87ad-447e-a8d5-ffd8dc612f9a/2c5a04b6e7fb29a1924bea251ffd6a6f I have no experience about the TikZ. So my PC always fails to compile it. My tex aversion is new. – Hinn Nov 13 '13 at 08:08