无法创建新命令来重新使用大块代码

无法创建新命令来重新使用大块代码

我是 LaTeX 新手。尝试创建一个可重复使用的命令来绘制卡诺表,但允许我随意为其提供更多内容。以下代码对我来说不起作用,失败并显示“未定义的控制序列”,但我不知道为什么,也无法让它工作,尝试了在互联网上找到的不同解决方案,包括一些使用xparse包的解决方案

\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

两个问题:

  1. 您需要用括号括住可选参数,因为它包含另一个可选参数。
  2. 您需要这样做,ampersand replacement因为&TikZ 矩阵有望找到该方法。

代码:

\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}

输出

您可以考虑使用像kvmapkarnaugh这样的允许可寻址矩阵节点的包并尝试简化绘制卡诺图的工作流程。

相关内容