0

I'm new to LaTeX. Trying to create a re-usable command drawing a Karnaugh table, but allowing me to optionally provide more content to it. The following piece of code doesn't work for me, failing with "Undefined control sequence", but I don't know why and can't get it to work trying different solution found on the internet, including some using xparse package

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{lmodern}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{multicol}

\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{matrix}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommand{\karnaugh}[1][]{%
  \begin{center}%
    \begin{tikzpicture}
      \matrix (karnaugh) [matrix of math nodes] {%
        0 & 0 & 0 & 1 \\%
        0 & 0 & 0 & 1 \\%
        - & - & - & - \\%
        0 & 1 & 0 & 0 \\%
      } ;

      \foreach \i/\bits in {1/00,2/01,3/11,4/10} {
        \node [left  = 1.2cm of karnaugh-\i-1] (x43-\i) {$\bits$} ;
        \node [above = 0.7cm of karnaugh-1-\i] (x21-\i) {$\bits$} ;
      }

      \node [above = .2cm of x43-1] (x4x3) {$x_4 x_3$} ;
      \node [left  = .2cm of x21-1] (x2x1) {$x_2 x_1$} ;

      \draw    ($(x4x3.north     -| karnaugh.west)  + ( -0.75mm, 6.00mm)$)
            -- ($(karnaugh.south -| karnaugh.west)  + ( -0.75mm, 0     )$)
               ($(x2x1.west      |- karnaugh.north) + (-10.00mm, 0.40mm)$)
            -- ($(karnaugh.east  |- karnaugh.north) + (     0  , 0.40mm)$)
               ($(karnaugh.north west)              + ( -0.75mm, 0.40mm)$)
            -- ($(x4x3.north     -| x2x1.west)      + (-10.00mm, 6.00mm)$);

      #1
    \end{tikzpicture}%
  \end{center}%
}

\begin{document}

Test

\karnaugh

\karnaugh[
  \begin{pgfonlayer}{background}
    \begin{scope}[opacity=.5]
      \fill[red](karnaugh-1-4.north west) rectangle (karnaugh-2-4.south east) ;
      \fill[blue](karnaugh-3-2.north west) rectangle (karnaugh-4-2.south east) ;
    \end{scope}
  \end{pgfonlayer}
]

\end{document}

1 Answer 1

2

Two problems:

  1. You need braces around your optional argument as it contains another optional argument.
  2. You need ampersand replacement because of the way & is expected to be found by TikZ matrices.

Code:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{lmodern}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{multicol}

\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{matrix}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommand{\karnaugh}[1][]{%
  \begin{center}%
    \begin{tikzpicture}[ampersand replacement=\&]
      \matrix (karnaugh) [matrix of math nodes] {%
        0 \& 0 \& 0 \& 1 \\%
        0 \& 0 \& 0 \& 1 \\%
        - \& - \& - \& - \\%
        0 \& 1 \& 0 \& 0 \\%
      } ;

      \foreach \i/\bits in {1/00,2/01,3/11,4/10} {
        \node [left  = 1.2cm of karnaugh-\i-1] (x43-\i) {$\bits$} ;
        \node [above = 0.7cm of karnaugh-1-\i] (x21-\i) {$\bits$} ;
      }

      \node [above = .2cm of x43-1] (x4x3) {$x_4 x_3$} ;
      \node [left  = .2cm of x21-1] (x2x1) {$x_2 x_1$} ;

      \draw    ($(x4x3.north     -| karnaugh.west)  + ( -0.75mm, 6.00mm)$)
            -- ($(karnaugh.south -| karnaugh.west)  + ( -0.75mm, 0     )$)
               ($(x2x1.west      |- karnaugh.north) + (-10.00mm, 0.40mm)$)
            -- ($(karnaugh.east  |- karnaugh.north) + (     0  , 0.40mm)$)
               ($(karnaugh.north west)              + ( -0.75mm, 0.40mm)$)
            -- ($(x4x3.north     -| x2x1.west)      + (-10.00mm, 6.00mm)$);

      #1
    \end{tikzpicture}%
  \end{center}%
}

\begin{document}

Test

\karnaugh

\karnaugh[{
  \begin{pgfonlayer}{background}
    \begin{scope}[opacity=.5]
      \fill[red](karnaugh-1-4.north west) rectangle (karnaugh-2-4.south east) ;
      \fill[blue](karnaugh-3-2.north west) rectangle (karnaugh-4-2.south east) ;
    \end{scope}
  \end{pgfonlayer}
}]

\end{document}

output

You may consider using packages like kvmap or karnaugh which allow addressable matrix nodes and try to simplify the workflow of drawing karnaugh maps.

1
  • Thank you! Will take a look at those packages
    – burtek
    Oct 25, 2021 at 11:16

You must log in to answer this question.

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