6

I need it to look exactly like this:

Photo of printed page

This is how far I got, but I’m stuck:

Photo of screen

\documentclass[10pt, a4paper] {article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage [Symbol]{upgreek}
\usepackage{tikz}

\begin{document}
\begin{figure}[!htb]
    \begin{tikzpicture}
    \draw[ultra thick] [ | ->] (0,4.4) --(3,4.4)
    \draw[fill=white] (4,4) rectangle (5,5);
    \begin{scope}[xshift=-1cm] \draw [fill=white] (4,4) rectangle (5,5);
    \end{scope}
    \end{tikzpicture}
    \end{figure}
\end{document}
TRiG
  • 213
  • 1
  • 12
  • 2
    Welcome to TeX.SE! Can you help us to help you by posting your code as text please? And making sure that it is a complete, compilable example? – Thruston Nov 19 '19 at 17:52
  • \begin{figure}[!htb] \begin{tikzpicture} \draw[ultra thick] [ | ->] (0,4.4) --(3,4.4) \draw[fill=white] (4,4) rectangle (5,5); \begin{scope}[xshift=-1cm] \draw [fill=white] (4,4) rectangle (5,5); \end{scope} \end{tikzpicture}

    \end{figure}

    – peachblossom Nov 19 '19 at 17:57
  • Do you mean like this? – peachblossom Nov 19 '19 at 17:58
  • 1
    Please remove the comment and edit the question... And be sure to include the \documentclass... parts. – Thruston Nov 19 '19 at 17:59

3 Answers3

10

This is to give you a welcome.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.multipart,chains}
\begin{document}
\begin{tikzpicture}[mp/.style args={#1/#2}{rectangle split,rectangle split horizontal,
rectangle split parts=2,text width=2em,align=center,minimum height=2.2em,
on chain,draw,node contents={#1\nodepart[text width=3em]{two}#2}}]
 \begin{scope}[start chain=going right,node distance=4em]
  \node[on chain](C) {C};
  \node (A1)[mp=4/~];
  \node (A2)[mp=4/~];
  \node (A3)[mp=6/Nil];
 \end{scope}
 \draw[very thick,|-latex] (C) -- (A1); 
 \foreach \X in {1,2}
 {\draw[very thick,|-latex] (A\X.two north|-A\X) -- (A\the\numexpr\X+1);}
\end{tikzpicture}
\end{document}

enter image description here

For the future, please always post the code that you have tried, never use screen shots (or do you really expect others to punch in the text from these), nor links to codes (because these links may die, and it is generally not a good idea to follow random links on the internet). Also your screen shot depicts a code fragment. However, it is expected that one posts complete documents that start with \documentclass, end with \end{document}, are as minimal as possible, and can be compiled.

And here is an annotated version. Clearly, annotations won't be as detailed as the manual.

\documentclass[tikz,border=3mm]{standalone}
% we load two libraries. shapes.multipart allows us to draw partitioned boxes
% and chains makes it possible to place nodes along chains 
\usetikzlibrary{shapes.multipart,chains}
\begin{document}
\begin{tikzpicture}[mp/.style args={#1/#2}{ %<- defines a style that takes 2 args
    rectangle split,%<- we want a partitioned rectangle
    rectangle split horizontal,%<- the partitions should be horizontal
    rectangle split parts=2,%<- two partitions, please
    text width=2em,%<- overall text width (we will change the second one
    align=center,%<- texts should be centered
    minimum height=2.2em,%<- selfexplantory
    on chain,%<- puts them on the chain
    draw,%<- draw node boundaries
    node contents={#1\nodepart[text width=3em]{two}#2}}
    ]% ^ here we specify how the arguments are to be used
    % whatever is before the / will go in the first part, 
    % what comes after in the second
 \begin{scope}[start chain=going right,% start a chain growing to the right
    node distance=4em]% distance between nodes 
  \node[on chain](C) {C};
  \node (A1)[mp=4/~]; %<- use the mp style to create a node of name A1
        % with first part 4 and second part just a space (standard trick)
  \node (A2)[mp=4/~];
  \node (A3)[mp=6/Nil];
 \end{scope}
 \draw[very thick,|-latex] (C) -- (A1); 
 \foreach \X in {1,2}
 {\draw[very thick,|-latex] (A\X.two north|-A\X) -- (A\the\numexpr\X+1);
  % ^ draws the arrow from the center of the right partition of node \X
  % to node \X+1 (we need to evaluate \X+1, hence \the\numexpr)
 }
\end{tikzpicture}
\end{document}

ADDENDUM: An extended comment/reply to lucky1928, who suggested to use join. It is a great idea but some extra treatment has to be applied to the first node.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.multipart,chains}
\begin{document}
\begin{tikzpicture}[mp/.style args={#1/#2}{rectangle split,rectangle split horizontal,
rectangle split parts=2,text width=2em,align=center,minimum height=2.2em,
on chain,draw,join,node contents={#1\nodepart[text width=3em]{two}#2}}]
 \begin{scope}[start chain=going right,node distance=4em,
 every join/.style={very thick,|-latex,to path={(\tikztostart.two north|-\tikztostart) -- (\tikztotarget)}}]
  \node[on chain,rectangle split,rectangle split horizontal,
    rectangle split parts=2,inner xsep=0.5pt](C) {C};
  \node (A1)[mp=4/~];
  \node (A2)[mp=4/~];
  \node (A3)[mp=6/Nil];
 \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks so much for help. Sorry im very new here, next time I will post code like you said. – peachblossom Nov 19 '19 at 18:01
  • 1
    @peachblossom No worries. You're welcome! –  Nov 19 '19 at 18:02
  • Can you tell me how you send you code like that on this site – peachblossom Nov 19 '19 at 18:03
  • 1
    @peachblossom You just copy it into the question, put it into the cursor (while editing) and then press the button that looks a bit like {}. This does the formatting. –  Nov 19 '19 at 18:04
  • wow thanks :) now it makes sense – peachblossom Nov 19 '19 at 18:04
  • If it wouldn't be too hard, could you explain shortly what each section means in the code? What you are doing. – peachblossom Nov 19 '19 at 18:08
  • 1
    @peachblossom Done. –  Nov 19 '19 at 18:18
  • Wow, thanks so much, I appreciate it allot :) I can't thank you enough! Helped me allot. – peachblossom Nov 19 '19 at 18:20
  • I actually have some questions on the steps you explained to me. What does "defines a style that takes 2 args" mean? "selfexplantory?" – peachblossom Nov 20 '19 at 00:21
  • 1
    @peachblossom You may think of it as a function (or macro) that takes two arguments and does something with them. In this case, the arguments are separated by a / (for no deep reason except that this can be more easily thrown under a /.list key but here I didn't use it). #1 is the place holder for the first argument, and #2 for the second. So if you say \node (A)[mp=pft/blub];, you will get pft in the first box and blub in the second. –  Nov 20 '19 at 00:26
  • 1
    @peachblossom selfexplantory is a typo, it should read selfexplanatory and just indicates that the minimum height height key specifies the minimal height of the corresponding node. In the beginning these keys may appear a bit strange, but I have used PSTricks for almost 20 years and produced hundreds of diagrams with MetaPost. After having seen what TikZ can do, I quit PSTricks completely and use MetaPost only for very specialized applications. After a very short time one can do so many things so easily, precisely because of the styles and relative positioning. –  Nov 20 '19 at 00:37
  • Thanks for the great explanations, appreciate it :) By the way, do you have any tips on sites that could help me understand LaTeX better? That is easy to understand for beginners. When making tables, draw pictures and eth with TikZ. – peachblossom Nov 20 '19 at 00:45
  • Would you mind writing a shortlist of basic commands that are good to know when drawing pictures with Tikz? The most mandatory to know. – peachblossom Nov 20 '19 at 00:50
  • @peachblossom I feel that his is beyond the scope of this question. This question has been asked here, and you can look at the answers or links. All I wanted to do is to write a welcome answer (after seeing the comments you got...). –  Nov 20 '19 at 00:52
  • Thanks for guiding me in the right direction, will look at the links. – peachblossom Nov 20 '19 at 00:54
  • Great answer, Is it possible to use join? I know now it will start from east and connect to west, not sure if it's possible to move join start point from center of right partition! – lucky1928 Nov 20 '19 at 03:18
  • @lucky1928 It should, with every join and to path. The problem, though, is that the start node is different so it is more efforts. If it was not for the C node, one could do every join/.style={very thick,|-latex,to path={(\tikztostart.two north|-\tikztostart) -- (\tikztotarget)}}. –  Nov 20 '19 at 03:21
  • @lucky1928 I added something that does it. It is a nice idea, thanks! –  Nov 20 '19 at 03:31
  • Hi! Here I am again on this thread. I do have a question. Is there a simpler way to make this picture with Tikz? Beginner friendly. If so could you show me the example? – peachblossom Dec 06 '19 at 17:56
  • @peachblossom I feel that this is simple, and I do not understand what you mean by "beginner friendly". Also I feel that there is some very peculiar sociology going on with your code, in particular by a user who downvoted me for using TikZ, so I am afraid that I am not adding anything here. If you ask a new question I will be happy to answer it, though. Please also consider accepting this answer if it solved your initial problem. –  Dec 06 '19 at 18:10
  • By beginner friendly I mean an easier way to code on tikz. Easier code. I have an assignment that I need to do for this particular shape. And it needs to be created by tikz. TikZ is very important. If this is the only way to do it. Then thanks for your help. – peachblossom Dec 06 '19 at 18:20
  • @peachblossom The way this site works is that you ask a new question. Asking questions is free. I feel I have provided you with a very reasonable answer to your original question. If you have a modified request, ask a new separate question. –  Dec 06 '19 at 18:31
  • Thank you, its clearer now how this site works. I thought it wasn't allowed to ask questions to a similar question I had asked. I thought you only could use one thread. – peachblossom Dec 06 '19 at 18:36
  • @peachblossom No, this is a public site. The idea is that the question and its answers are for everyone. If I were to modify the answer, it may become less useful for others. –  Dec 06 '19 at 18:40
  • Thanks! Now I understand. Will make a new thread then. – peachblossom Dec 06 '19 at 18:42
10

And just so you know that you don't have to use TikZ, here's an alternative in Metapost. You will need to compile this example with the lualatex version of TeX.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
def drawtailarrow expr p = _apth := p; _fintarr enddef;
def _fintarr text t =
  draw (up--down) scaled 3 shifted point 0 of _apth t;
  draw _apth t;
  filldraw arrowhead _apth t
enddef;
beginfig(1);
    numeric ht, wd; ht = 21; wd = 34;

    path box[];
    box1 = unitsquare xscaled wd yscaled ht;
    box3 = box1 shifted (3 wd, 0);
    box5 = box1 shifted (6 wd, 0);

    box2 = unitsquare scaled ht shifted point 1 of box1;
    box4 = unitsquare scaled ht shifted point 1 of box3;
    box6 = unitsquare scaled ht shifted point 1 of box5;

    z0 = center box1 shifted (-3/2 wd, 0);
    label("$C$", z0);
    label("$4$", center box1);
    label("$6$", center box3);
    label("$6$", center box5);
    label("Nil", center box6);

    forsuffixes $=1,2,3,4,5,6: draw box$ withcolor 3/4 blue; endfor

    interim linecap := 0;  % these parameters improve the look of 
    interim linejoin := 0; % the arrows drawn with a fat pen
    interim ahangle := 30;

    drawoptions(withpen pencircle scaled 1.2 withcolor 2/3 red);
    drawtailarrow z0 shifted 10 right -- point 7/2 of box1;
    drawtailarrow center box2 -- point 7/2 of box3;
    drawtailarrow center box4 -- point 7/2 of box5;
    drawoptions();

endfig;
\end{mplibcode}
\end{document}
Thruston
  • 42,268
  • Thanks for your dedication to providing good metapost answers Thruston. To anyone reading, while this answer might look slightly longer, it also saves loading several thousand (?) lines of code/predefined macros in the background. – Scott H. Nov 20 '19 at 06:24
  • 1
    @ScottH. It is certainly true that it loads less packages, but personally I feel that one should also add that it relies on external code that has many many lines. You can always create some external software and thus keep the number of lines that LaTeX loads small. But it does not mean that the code in the background is small. Also one could argue that, unfortunately, as of now arXiv does not yet support lualatex, so it can be advantageous to rely on LaTeX packages only, which also do not require the -shell-escape flag. –  Nov 20 '19 at 20:08
5

Here is a way with pstricks:

\documentclass{article}
\usepackage{geometry}
\usepackage{array, bigstrut}
\newcommand{\mybox}[1]{%
\setlength{\bigstrutjot}{1.5ex}
\begin{tabular}{|>{\centering}p{3em}|>{\centering\arraybackslash}p{1.6em}|}%
\hline
\bigstrut #1 \tabularnewline
\hline
\end{tabular}
}%

\usepackage{pst-node, pst-arrow, auto-pst-pdf}

\begin{document}

\begin{psmatrix}[colsep=1.2cm]
  [name =C]C &\mybox{4 & \pnode[0, 0.7ex]{A}} & \mybox{ 6 & \pnode[0, 0.7ex]{B}} & \mybox{ 6 & Nil}
\psset{linewidth=1.33pt, arrows=|-v, veearrowangle=60, veearrowlength=2mm, veearrowlinewidth=1.33pt, linejoin=1, nodesepB=-2.67pt}
\ncline[nodesepA=0.75em]{C}{1,2}\ncline{A}{1,3}\ncline{B}{1,4}
\end{psmatrix}

\end{document} 

enter image description here

Bernard
  • 271,350