I have a question: how do I use latex so that it can replicate this structure (latex)?

enter image description here

Unfortunately I don't know the libraries very well. It would be useful for me to understand how to draw certain diagrams to draw other lattices very similar to this one in the future.

  • 1
    You can use tikz-cd for that.
    – user194703
    Nov 27 '19 at 16:32

With pure tikz, quite elementary with use of the libraries calc, chains and positioning:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{calc, chains, 

node distance = 8mm and 2mm,
  start chain = going right,
     N/.style = {inner sep=1pt, on chain}
\node (n21) [N] {$\{1,2\}$};
\node (n22) [N] {$\{1,3\}$};
\node (n23) [N] {$\{1,4\}$};
\node (n24) [N] {$\{2,3\}$};
\node (n25) [N] {$\{2,4\}$};
\node (n26) [N] {$\{3,4\}$};
\node (n11) [N, below=of n21]                           {$\{1\}$};
\node (n12) [N, below=of $(n22.south)!0.5!(n23.south)$] {$\{2\}$};
\node (n13) [N, below=of $(n24.south)!0.5!(n25.south)$] {$\{3\}$};
\node (n14) [N, below=of n26]                           {$\{4\}$};
\draw (n11) -- (n21)    (n11) -- (n22)
      (n12) -- (n21)    (n12) -- (n24)  (n12) -- (n25)
      (n13) -- (n22)    (n13) -- (n24)  (n13) -- (n26)
      (n14) -- (n23)    (n14) -- (n25)  (n14) -- (n26);
\draw[red, very thick]  (n11) -- (n23);
\node (n31) [N, above=of n21]               {$\{1,2,3\}$};
\node (n32) [N, above=of n21.north -| n12]  {$\{1,2,4\}$};
\node (n33) [N, above=of n21.north -| n13]  {$\{1,3,4\}$};
\node (n34) [N, above=of n26]               {$\{2,3,4\}$};
\draw (n31) -- (n21)    (n31) -- (n22)  (n31) -- (n23)
      (n32) -- (n21)    (n32) -- (n23)  (n32) -- (n25)
      (n33) -- (n23)    (n33) -- (n26)
      (n34) -- (n24)    (n34) -- (n25)  (n34) -- (n26);
\draw[red, very thick]  (n33) -- (n23);
\node (n41) [N, above=of $(n32.north)!0.5!(n33.north)$] {$\{1,2,3,4\}$};
\draw (n41) -- (n31)    (n41) -- (n32)  (n41) -- (n34);
\draw[red, very thick]  (n41) -- (n33);

enter image description here


This diagram seems to have the following logic: one starts with {1,2,3,4} and finds all subsets of that list that emerge by dropping one entry, and puts them in the next line. In the line below that, one puts all subsets of these, which emerge by dropping another element. Continuing like this, one arrives at one-element lists in the last line.

A given list gets connected with a list on the upper level if it is fully contained in that list. One may wonder if one can make LaTeX decide where one should draw these connecting line. The answer is


Of course, this requires some preparations, like a membership test, which is available e.g. here, and a function that finds the intersection of two list, which I added here. The result is

 \foreach \X [count=\Y,remember=\X as \LastX,remember=\myn as \mylastn] in {{"1,2,3,4"},{"1,2,3","1,2,4","1,3,4","2,3,4"},%
    \node (L-1-1) at (0,0) {$\{\myfirst\}$};
   \node (L-\Y-1) at (-4,1.5-\Y*1.5) {$\{\myfirst\}$};
   \node (L-\Y-\the\numexpr\myn+1) at (4,1.5-\Y*1.5) {$\{\mylast\}$};
   \path (L-\Y-1.center) -- (L-\Y-\the\numexpr\myn+1\relax.center) 
    foreach \Z in {1,...,\the\numexpr\myn-1}
        node[pos=\Z/\myn] (L-\Y-\the\numexpr\Z+1) {$\{\myentry\}$} };
     \foreach \Z in {0,...,\the\numexpr\myn}
     {\draw (L-\Y-\the\numexpr\Z+1) -- (L-1-1);}
     \foreach \Z in {0,...,\the\numexpr\myn}        
       \draw (L-\Y-\the\numexpr\Z+1) -- 
 \draw[red,very thick]  (L-1-1) -- (L-2-3) -- (L-3-3) -- (L-4-1);


enter image description here

The only thing that is hard coded is the red line, for which I didn't see any pattern. However, drawing this is as simple as

\draw[red,very thick]  (L-1-1) -- (L-2-3) -- (L-3-3) -- (L-4-1);

The advantage is that this is applicable to other similar diagrams, and one does not have to draw things by hand. (For instance, as of now, in the other answer the connection between {1,3,4} and {1,3} is missing. If I had to draw these by paws I would most likely miss more connections.)

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.