Skip to content

Instantly share code, notes, and snippets.

@masawada
Last active January 14, 2022 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save masawada/3ca01756080e03ce3c34 to your computer and use it in GitHub Desktop.
Save masawada/3ca01756080e03ce3c34 to your computer and use it in GitHub Desktop.
LaTeXでカルノー図
\documentclass[a4j, 10pt, dvipdfmx]{jsarticle}
\usepackage[dvipdfmx]{graphicx}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
% ref: http://tex.stackexchange.com/questions/36798/remove-vertical-lines-for-table/36879#36879
%internal group
%#1-space between node and grouping line. Default=0
%#2-top left node
%#3-bottom right node
\newcommand{\implicant}[3][0]{
\draw[rounded corners=3pt] ($(#2.north west)+(135:#1)$) rectangle ($(#3.south east)+(-45:#1)$);
}
%group lateral borders
%#1-space between node and grouping line. Default=0
%#2-top left node
%#3-bottom right node
\newcommand{\implicantcostats}[3][0]{
\draw[rounded corners=3pt] ($(rf.east |- #2.north)+(90:#1)$)-| ($(#2.east)+(0:#1)$) |- ($(rf.east |- #3.south)+(-90:#1)$);
\draw[rounded corners=3pt] ($(cf.west |- #2.north)+(90:#1)$) -| ($(#3.west)+(180:#1)$) |- ($(cf.west |- #3.south)+(-90:#1)$);
}
%group top-bottom borders
%#1-space between node and grouping line. Default=0
%#2-top left node
%#3-bottom right node
\newcommand{\implicantdaltbaix}[3][0]{
\draw[rounded corners=3pt] ($(cf.south -| #2.west)+(180:#1)$) |- ($(#2.south)+(-90:#1)$) -| ($(cf.south -| #3.east)+(0:#1)$);
\draw[rounded corners=3pt] ($(rf.north -| #2.west)+(180:#1)$) |- ($(#3.north)+(90:#1)$) -| ($(rf.north -| #3.east)+(0:#1)$);
}
%group corners
%#1-space between node and grouping line. Default=0
\newcommand{\implicantcantons}[1][0]{
\draw[rounded corners=3pt] ($(rf.east |- 0.south)+(-90:#1)$) -| ($(0.east |- cf.south)+(0:#1)$);
\draw[rounded corners=3pt] ($(rf.east |- 8.north)+(90:#1)$) -| ($(8.east |- rf.north)+(0:#1)$);
\draw[rounded corners=3pt] ($(cf.west |- 2.south)+(-90:#1)$) -| ($(2.west |- cf.south)+(180:#1)$);
\draw[rounded corners=3pt] ($(cf.west |- 10.north)+(90:#1)$) -| ($(10.west |- rf.north)+(180:#1)$);
}
%Empty Karnaugh map 4x4
\newenvironment{Karnaugh}[2]%
{
\begin{tikzpicture}[baseline=(current bounding box.north),scale=0.8]
\draw (0,0) grid (4,4);
\draw (0,4) -- node [pos=0.8,above right,anchor=south west] {#1} node [pos=0.7,below left,anchor=north east] {#2} ++(135:1);
%
\matrix (mapa) [matrix of nodes,
column sep={0.8cm,between origins},
row sep={0.8cm,between origins},
every node/.style={minimum size=0.3mm},
anchor=8.center,
ampersand replacement=\&] at (3.5,3.5)
{
\& |(c00)| 00 \& |(c01)| 01 \& |(c11)| 11 \& |(c10)| 10 \& |(cf)| \phantom{00} \\
|(r00)| 00 \& |(0)| \phantom{0} \& |(4)| \phantom{0} \& |(12)| \phantom{0} \& |(8)| \phantom{0} \& \\
|(r01)| 01 \& |(1)| \phantom{0} \& |(5)| \phantom{0} \& |(13)| \phantom{0} \& |(9)| \phantom{0} \& \\
|(r11)| 11 \& |(3)| \phantom{0} \& |(7)| \phantom{0} \& |(15)| \phantom{0} \& |(11)| \phantom{0} \& \\
|(r10)| 10 \& |(2)| \phantom{0} \& |(6)| \phantom{0} \& |(14)| \phantom{0} \& |(10)| \phantom{0} \& \\
|(rf) | \phantom{00} \& \& \& \& \& \\
};
}%
{
\end{tikzpicture}
}
\newenvironment{Karnaughvuit}[2]%
{
\begin{tikzpicture}[baseline=(current bounding box.north),scale=0.8]
\draw (0,0) grid (4,2);
\draw (0,2) -- node [pos=0.7,above right,anchor=south west] {#1} node [pos=0.7,below left,anchor=north east] {#2} ++(135:1);
%
\matrix (mapa) [matrix of nodes,
column sep={0.8cm,between origins},
row sep={0.8cm,between origins},
every node/.style={minimum size=0.3mm},
anchor=4.center,
ampersand replacement=\&] at (3.5,1.5)
{
\& |(c00)| 00 \& |(c01)| 01 \& |(c11)| 11 \& |(c10)| 10 \& |(cf)| \phantom{00} \\
|(r00)| 0 \& |(0)| \phantom{0} \& |(2)| \phantom{0} \& |(6)| \phantom{0} \& |(4)| \phantom{0} \& \\
|(r01)| 1 \& |(1)| \phantom{0} \& |(3)| \phantom{0} \& |(7)| \phantom{0} \& |(5)| \phantom{0} \& \\
|(rf) | \phantom{00} \& \& \& \& \& \\
};
}%
{
\end{tikzpicture}
}
%Defines 16 values (0,1,X)
\newcommand{\contingut}[1]{%
\foreach \x [count=\xi from 0] in {#1}
\path (\xi) node {\x};
}
\begin{document}
\begin{center}
\begin{figure}
\centering
\begin{Karnaugh}{$\tt{x_1 x_0}$}{$\tt{y_0 y_0}$}
\contingut{0,0,0,0,1,0,0,0,1,1,0,0,1,1,1,0}
\implicant{4}{12}
\implicant[3pt]{12}{9}
\implicantdaltbaix[4pt]{12}{14}
\end{Karnaugh}
\caption{カルノー図 4変数}
\end{figure}
\end{center}
\begin{center}
\begin{figure}
\centering
\begin{Karnaughvuit}{ab}{c}
\contingut{0,1,1,0,0,1,1,0}
\implicant{2}{6}
\implicantcostats{1}{5}
\end{Karnaughvuit}
\caption{カルノー図 3変数}
\end{figure}
\end{center}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment