TransWikia.com

How draw 2 things next each other?

TeX - LaTeX Asked by sherek_66 on January 10, 2021

How I can draw 2 cubes next each other? I mean two cubes like this but next to each other:

documentclass{standalone}

usepackage{tikz}
    usetikzlibrary{calc}
    usetikzlibrary{patterns}

begin{document}
    
    begin{tikzpicture}
    
        foreach n/x/l/p in{
            1/{(0, 4)}/{$1$}/left,
            2/{(4, 4)}/{$2$}/south west,
            3/{(0, 0)}/{$3$}/below,
            4/{(4, 0)}/{$4$}/below,
            5/{(0.8, 4.8)}/{$5$}/above,
            6/{(4.8, 4.8)}/{$6$}/above,
            7/{(.8, 1)}/{$7$}/south east,
            8/{(4.8, 1)}/{$8$}/right
        }{
            node[inner sep=2pt,circle,draw,fill,label={p:l}] (n) at x {};
        }
        draw (1) -- (2) -- (4) -- (3) -- cycle;
        draw (1) -- (2) -- (6) -- (5) -- cycle;
        draw (2) -- (4) -- (8) -- (6) -- cycle;
        draw (3) -- (1) -- (5);
        draw[dashed] (5) -- (7) -- (3);
        draw[dashed] (7) -- (8);
        
        draw[pattern=north west lines] ($(1)!0.5!(3)$) -- ($(5)!0.5!(7)$) -- ($(6)!0.5!(8)$) -- ($(2)!0.5!(4)$) -- cycle;
        
        node (a1) at ($(3)!0.5!(4) - (0,0.5)$) {$1rightarrow3$};
        node (a2) at ($(a1) - (0,0.5)$) {$5rightarrow7$};
        node (a3) at ($(a2) - (0,0.5)$) {$6rightarrow8$};
        node (a4) at ($(a3) - (0,0.5)$) {$2rightarrow4$};
    
    end{tikzpicture}
    
end{document}

2 Answers

A "brute force" method would be to repeat all the code, inside the same tikzpicture, but increase all x-coordinates. That's rather inconvenient though, it's easier to use

begin{scope}[xshift=6cm]
<code for cube>
end{scope}

A third option might be to use two tikzpicture environment, one immediately after the other. Below I used the scope approach.

enter image description here

documentclass{standalone}

usepackage{tikz}
    usetikzlibrary{calc}
    usetikzlibrary{patterns}

begin{document}
    
    begin{tikzpicture}
    
        foreach n/x/l/p in{
            1/{(0, 4)}/{$1$}/left,
            2/{(4, 4)}/{$2$}/south west,
            3/{(0, 0)}/{$3$}/below,
            4/{(4, 0)}/{$4$}/below,
            5/{(0.8, 4.8)}/{$5$}/above,
            6/{(4.8, 4.8)}/{$6$}/above,
            7/{(.8, 1)}/{$7$}/south east,
            8/{(4.8, 1)}/{$8$}/right
        }{
            node[inner sep=2pt,circle,draw,fill,label={p:l}] (n) at x {};
        }
        draw (1) -- (2) -- (4) -- (3) -- cycle;
        draw (1) -- (2) -- (6) -- (5) -- cycle;
        draw (2) -- (4) -- (8) -- (6) -- cycle;
        draw (3) -- (1) -- (5);
        draw[dashed] (5) -- (7) -- (3);
        draw[dashed] (7) -- (8);
        
        draw[pattern=north west lines] ($(1)!0.5!(3)$) -- ($(5)!0.5!(7)$) -- ($(6)!0.5!(8)$) -- ($(2)!0.5!(4)$) -- cycle;
        
        node (a1) at ($(3)!0.5!(4) - (0,0.5)$) {$1rightarrow3$};
        node (a2) at ($(a1) - (0,0.5)$) {$5rightarrow7$};
        node (a3) at ($(a2) - (0,0.5)$) {$6rightarrow8$};
        node (a4) at ($(a3) - (0,0.5)$) {$2rightarrow4$};
        
    
    begin{scope}[xshift=6cm]
        
        foreach n/x/l/p in{
            1/{(0, 4)}/{$1$}/left,
            2/{(4, 4)}/{$2$}/south west,
            3/{(0, 0)}/{$3$}/below,
            4/{(4, 0)}/{$4$}/below,
            5/{(0.8, 4.8)}/{$5$}/above,
            6/{(4.8, 4.8)}/{$6$}/above,
            7/{(.8, 1)}/{$7$}/south east,
            8/{(4.8, 1)}/{$8$}/right
        }{
            node[inner sep=2pt,circle,draw,fill,label={p:l}] (n) at x {};
        }
        draw (1) -- (2) -- (4) -- (3) -- cycle;
        draw (1) -- (2) -- (6) -- (5) -- cycle;
        draw (2) -- (4) -- (8) -- (6) -- cycle;
        draw (3) -- (1) -- (5);
        draw[dashed] (5) -- (7) -- (3);
        draw[dashed] (7) -- (8);
        
        draw[pattern=north west lines] ($(1)!0.5!(3)$) -- ($(5)!0.5!(7)$) -- ($(6)!0.5!(8)$) -- ($(2)!0.5!(4)$) -- cycle;
        
        node (a1) at ($(3)!0.5!(4) - (0,0.5)$) {$1rightarrow3$};
        node (a2) at ($(a1) - (0,0.5)$) {$5rightarrow7$};
        node (a3) at ($(a2) - (0,0.5)$) {$6rightarrow8$};
        node (a4) at ($(a3) - (0,0.5)$) {$2rightarrow4$};
    
    end{scope}
    
    end{tikzpicture}
    
end{document}

Correct answer by Torbjørn T. on January 10, 2021

You can also use saveboxes or pics.

documentclass{standalone}

usepackage{tikz}
    usetikzlibrary{calc}
    usetikzlibrary{patterns}
    
newsavebox{tempbox}

begin{document}

 savebox{tempbox}{begin{tikzpicture}
    
        foreach n/x/l/p in{
            1/{(0, 4)}/{$1$}/left,
            2/{(4, 4)}/{$2$}/south west,
            3/{(0, 0)}/{$3$}/below,
            4/{(4, 0)}/{$4$}/below,
            5/{(0.8, 4.8)}/{$5$}/above,
            6/{(4.8, 4.8)}/{$6$}/above,
            7/{(.8, 1)}/{$7$}/south east,
            8/{(4.8, 1)}/{$8$}/right
        }{
            node[inner sep=2pt,circle,draw,fill,label={p:l}] (n) at x {};
        }
        draw (1) -- (2) -- (4) -- (3) -- cycle;
        draw (1) -- (2) -- (6) -- (5) -- cycle;
        draw (2) -- (4) -- (8) -- (6) -- cycle;
        draw (3) -- (1) -- (5);
        draw[dashed] (5) -- (7) -- (3);
        draw[dashed] (7) -- (8);
        
        draw[pattern=north west lines] ($(1)!0.5!(3)$) -- ($(5)!0.5!(7)$) -- ($(6)!0.5!(8)$) -- ($(2)!0.5!(4)$) -- cycle;
        
        node (a1) at ($(3)!0.5!(4) - (0,0.5)$) {$1rightarrow3$};
        node (a2) at ($(a1) - (0,0.5)$) {$5rightarrow7$};
        node (a3) at ($(a2) - (0,0.5)$) {$6rightarrow8$};
        node (a4) at ($(a3) - (0,0.5)$) {$2rightarrow4$};
    
end{tikzpicture}}%
usebox{tempbox}quadusebox{tempbox}quadusebox{tempbox}
    
end{document}

Answered by John Kormylo on January 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP