10

I'm trying to draw the following picture in LaTeX but I'm having trouble understanding what I should use. I've seen a few examples, but it's hard to adapt them into what I'm looking for.

enter image description here

If you could help, I'd be truly grateful.

  • Dear Helena, i would first decide with which package i will draw, then would search for similar question here on site and then try to accommodate the similarities to my needs ... see texample.net. if is there something usable for you – Zarko Oct 22 '17 at 18:44
  • 7
    @KurtK I think OP made it pretty clear he doesn't know where to start. I don't think the Show us your code canned response applies when the asker admits to not even knowing how to approach the problem. – user1717828 Oct 22 '17 at 23:23
  • 3
    @jon we aim to please! – Hel Oct 23 '17 at 10:49
15

Welcome to TeX.SE!

Here is a simple prototype of what you need. Hopefully, you may apply your desired customizations to it.

\documentclass[border=1pt]{standalone}
\usepackage{tikz}  %TikZ central library is called.
\usetikzlibrary{automata,positioning} % automata and positioning libraries are required to use nodes and coordinates in addition to placement propetries.
\begin{document}
    \begin{tikzpicture}[shorten >=1pt,node distance=1.0cm,on grid,auto] % Some customizations related to the size and the discatnce between nodes and arrow heads
    \node[state,rectangle, align=center] (q_r) [] {This is a \\ square}; % Here the nodes and coordinates are defined
    \node[coordinate] (q_0) [right=of q_r, xshift=3cm]   {};
    \node[coordinate] (q_1) [left=of q_r, xshift=-3cm, yshift=1mm]   {};
    \node[coordinate] (q_2) [left=of q_r, xshift=-3cm, yshift=-1mm]   {};
    \path[->] % path and draw commands connect the nodes and coordinates to each other.
    (q_r) edge [] node  {This is an arrow} (q_0);   
    \draw[->] ([yshift=-3mm]q_1) -- ([yshift=-2mm]q_r.west) node[midway,swap] {This is an arrow};
    \draw[->] ([yshift=3mm]q_2) -- ([yshift=2mm]q_r.west) node[midway] {This is an arrow};
    \end{tikzpicture}
\end{document}  

enter image description here

13

For complicated figures with graphics, I think there is some consensus that tikz is the way to go. It is probably worth learning. It is amazing, but can be intimidating for the beginner. For very simple pictures (lines, arrows, text, ovals) the picture environment has easy-to-learn tools. The \put command together with \line and \vector commands can recreate your picture.

The basic idea is to \put the objects at coordinates relative to the current baseline position. The downside is that small adjustments require recalculating all your coordinates. Lengths and coordinates are in terms of \unitlength.

For \line and \vector commands the format is

\line(∆x,∆y){length}

The ∆x and ∆y arguments specify the slope (and direction) for lines (and vectors).

Here is the code for an approximation of your picture:

\documentclass{article}

\begin{document}

\noindent I am trying to draw something like this:

\bigskip
\begin{picture}(240,50) % set dimensions of the picture
\put(10,10){\vector(1,0){100}} % lower arrow
\put(30,1){\scriptsize This is an arrow}
\put(10,40){\vector(1,0){100}} % upper arrow
\put(30,44){\scriptsize This is an arrow}
\put(110,0){\line(1,0){120}} % these 4 lines make a rectangle
\put(230,0){\line(0,1){50}}
\put(110,0){\line(0,1){50}}
\put(110,50){\line(1,0){120}}
\put(230,25){\vector(1,0){100}} % right arrow
\put(250,29){\scriptsize This is an arrow}
\put(142,30){\Large \sffamily This is a}
\put(150,10){\Large \sffamily square}
\end{picture}

\bigskip
\noindent More text.

\end{document}

enter image description here

  • 2
    You're using commands from the picture environment, not from the graphics package. – Bernard Oct 22 '17 at 22:59
  • It doesn't really matter for this simple example, but in general the xpicture package has more functionality and fewer arbitrary restrictions than the original picture environment. But picture has the slight advantage that actually built into the base LaTeX code, so it is available from (and compatible with) every document class in your LaTeX distribution. – alephzero Oct 23 '17 at 4:32
7

for joy (exploiting answer of Roboticist as "your" mwe):

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{arrows.meta, positioning, quotes}% used tikz libraries

\begin{document}
    \begin{tikzpicture}[
    auto,                            % positioning of edges labels
    node distance = 2.5 mm and 40mm, % vertical and horizontal distances between node/coordinates
       box/.style = {rectangle, draw, align=center}% style of node
                        ]
    \node (n1) [box]    {This is\\ a square}; % node with style "box"
    % used coordinates 
    \coordinate[above left=of n1.west]  (in1); 
    \coordinate[below left=of n1.west]  (in2);
    \coordinate[right=of n1.east]       (out);
    arrows
    \draw[-Straight Barb]   % arrows head determined by arrows.meta
        (in1) edge ["This is an arrow"  ] (in1 -| n1.west)
        (in2) edge ["This is an arrow" '] (in2 -| n1.west)% ' means "swap" edge label on another side of adge
        (n1.east) to ["This is an arrow"  ] (out);
    \end{tikzpicture}
\end{document} 

code should be self-explanatory ... now i add comments, which describe meaning of using of some code lines. of course, for mastering of tikz it is good to read tikz & pgf manual ..., at least introduction parts (1. tutorial) and then TikZ ist kein Zeichenprogramm.

enter image description here

7

A short and simple code with pstricks:

\documentclass[border=5pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pst-node} 
\usepackage{auto-pst-pdf} % To compile with pdflatex -shell-escape (TeX Live, MacTeX)%
                                      %  or pdflatex --enable-write18 (MiKTeX)
\begin{document}

\psset{arrowinset=0, arrows =->}
\everypsbox{\sffamily \small}
\begin{psmatrix}[mnode=r, colsep=3cm]
\sffamily
\pnode{In} & \psDefBoxNodes{Sq}{\fboxsep=10pt\fbox{\parbox{4cm}{\centering\Large\bfseries This is a \\ square }}} &\pnode{Out}
\ncline[offset=8pt]{In}{Sq:Cl}\naput{This is an arrow}
\ncline[offset=-8pt]{In}{Sq:Cl}\nbput{This is an arrow}
\ncline{Sq:Cr}{Out}\naput{This is an arrow}
\end{psmatrix}

\end{document}

enter image description here

3

This could be a job for MetaPost as well:

\documentclass{article}
\usepackage{luamplib, array}
\begin{document}
\begin{mplibcode}
    input boxes;
    beginfig(1);
        boxit.sqr(btex \begin{tabular}{>{\large\sffamily}c}This is a\\ square\end{tabular}etex);
        drawboxed(sqr);
        z = (3cm, 0);
        drawarrow  sqr.e -- (sqr.e + z);
        label.top (btex This is an arrow etex, sqr.e + .5z);
        pair A[], B[], dir;
        dir = (0, ypart .5[sqr.w, sqr.nw]);
        for i = -1, 1:
            B[i] = sqr.w + i*dir; A[i] = B[i] - z;
            drawarrow A[i] -- B[i];
            label(btex This is an arrow etex, .5[A[i], B[i]]+(0, 2i*labeloffset));
        endfor;
    endfig;
\end{mplibcode}
\end{document}

To be typeset with LuaLaTeX.

enter image description here

0

You can also draw the picture in Inkscape and export the file as TiKz code using the TiKz Export extension.

0

My suggestion is to use an application with a GUI, ie., Dia. The application can export to LATEX PGF macro's. "Dia is a GTK+ based diagram creation program for GNU/Linux, MacOS X, Unix, and Windows, and is released under the GPL license." (https://wiki.gnome.org/action/show/Apps/Dia?action=show&redirect=Dia).

You can easily include the diagram as follows in your document, for example:

\usepackage{tikz}

\begin{document}

    \vspace{1em}
    \begin{figure}[bth]
        \centering
        \input{this-is-a-square.pgf}
        \caption[This is a square]{This is a square.} 
        \label{fig:thisisasquare}
    \end{figure}

\end{document}

The pgf file below contains the thisisasquare figure drawn with Dia. Note that the size of the text "This is a square" can be adjusted with the scale property [framed,scale=0.7,every node/.style={scale=0.8}].

\ifx\du\undefined
  \newlength{\du}
\fi
\setlength{\du}{15\unitlength}
\begin{tikzpicture}[framed,scale=0.7,every node/.style={scale=0.8}]
\pgftransformxscale{1.000000}
\pgftransformyscale{-1.000000}
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (12.400000\du,7.000000\du)--(12.400000\du,12.000000\du)--(25.150000\du,12.000000\du)--(25.150000\du,7.000000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (12.400000\du,7.000000\du)--(12.400000\du,12.000000\du)--(25.150000\du,12.000000\du)--(25.150000\du,7.000000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (18.775000\du,9.295000\du){This is a};
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (18.775000\du,10.095000\du){square};
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetbuttcap
{
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetfillcolor{dialinecolor}
% was here!!!
\pgfsetarrowsend{stealth}
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (5.500000\du,8.000000\du)--(12.350000\du,7.950000\du);
}
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetbuttcap
{
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetfillcolor{dialinecolor}
% was here!!!
\pgfsetarrowsend{stealth}
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (5.455364\du,11.187328\du)--(12.305364\du,11.137328\du);
}
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetbuttcap
{
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetfillcolor{dialinecolor}
% was here!!!
\pgfsetarrowsend{stealth}
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (25.150000\du,9.500000\du)--(32.055364\du,9.437328\du);
}
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node[anchor=west] at (6.500000\du,12.700000\du){This is an arrow};
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node[anchor=west] at (6.055000\du,7.025000\du){This is an arrow};
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node[anchor=west] at (25.855000\du,8.625000\du){This is an arrow};
\end{tikzpicture}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.