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

3

2 Answers 2

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

12
  • You should not repeat |<->| as it will violate the DRY (Don't Repeat Yourself) principle. :-) Nov 13, 2013 at 8: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. :-)
    – user11232
    Nov 13, 2013 at 9: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, 2013 at 9:12
  • 1
    @Hinn: How would you like it? 1. adding a node or 2. using figure environment?
    – user11232
    Nov 13, 2013 at 9:50
  • @HarishKumar I like both of them. Please see my question for it evernote.com/shard/s192/sh/1cb3ab4d-27bc-412d-9e47-f9cb7be0cfb3/… Thanks!
    – Hinn
    Nov 13, 2013 at 14:24
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

4
  • 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, 2013 at 7: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, 2013 at 7:50
  • Not the animation, but the frame around the image. :-)
    – user11232
    Nov 13, 2013 at 7:51
  • @HarishKumar Marienplatz Jake. Thank you first, please see evernote.com/shard/s192/sh/5c8110ab-87ad-447e-a8d5-ffd8dc612f9a/… I have no experience about the TikZ. So my PC always fails to compile it. My tex aversion is new.
    – Hinn
    Nov 13, 2013 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .