版本 B

版本 B

考虑以下 MWE:

\documentclass[border=5pt,tikz]{standalone}
\usepackage{ifthen}
\newcommand{\binary}[3]{
    \pgfmathsetmacro\number{int(#1*2^0+#2*2^1+#3*2^2)}
}
\newcommand{\sieben}[1]{

    \draw[gray,opacity=.3,line width=3pt] (0,0) --+ (0,.5);
    \draw[gray,opacity=.3,line width=3pt] (0,.6) --+ (0,.5);
    \draw[xshift=.7cm,gray,opacity=.3,line width=3pt] (0,0) --+ (0,.5);
    \draw[xshift=.7cm,gray,opacity=.3,line width=3pt] (0,.6) --+ (0,.5);
    \draw[gray,opacity=.3,line width=3pt] (.1,1.18) --+ (.5,0);
    \draw[gray,opacity=.3,line width=3pt] (.1,-.05) --+ (.5,0);
    \draw[gray,opacity=.3,line width=3pt] (.1,.55) --+ (.5,0);

    \ifthenelse{\equal{#1}{0}}{
        \draw[red,line width=3pt] (0,0) --+ (0,.5);
        \draw[red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
        \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{1}}{
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
    }{}

    \ifthenelse{\equal{#1}{2}}{
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);;
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
        \draw[red,line width=3pt] (0,0) --+ (0,.5);
        \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{3}}{
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
        \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
        \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{4}}{
        \draw[red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{5}}{
        \draw[red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
        \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
        \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{6}}{
            \draw[red,line width=3pt] (0,0) --+ (0,.5);
        \draw[red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
        \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
        \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{7}}{
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{8}}{
        \draw[red,line width=3pt] (0,0) --+ (0,.5);
        \draw[red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
        \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
        \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{9}}{
        \draw[red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
        \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
        \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
    }{}

    \ifthenelse{\equal{#1}{a}}{
        \draw[red,line width=3pt] (0,0) --+ (0,.5);
        \draw[red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
        \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
        \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
        \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
    }{}

%   \ifnum#1>9
%       \draw[red,line width=3pt] (0,0) --+ (0,.5);
%       \draw[red,line width=3pt] (0,.6) --+ (0,.5);
%       \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
%       \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
%       \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
%   \fi

}
\begin{document}
    \begin{tikzpicture}[xslant=0]
        \sieben{a}
        \begin{scope}[xshift=1.5cm]
            \binary{1}{1}{1} % seven in dual system
            \sieben{\number}
        \end{scope}
    \end{tikzpicture}
\end{document}

输出如下:

截屏

我的问题是:当我取消注释命令 ( ) 定义中的注释部分\ifnum#1>9 …并输入字母“a”作为参数时,会出现错误。但是当我注释此部分时,就不会出现错误。

我的问题是:我该如何修复这个错误?

答案1

我很可能不明白你的问题。但我想说的是,应该首先检查参数是否为数字(我只检查它是否为数字,a但这可以扩展),然后将其用于\ifcase整数情况。

\documentclass[border=5pt,tikz]{standalone}
\usepackage{ifthen}
\newcommand{\binary}[3]{
    \pgfmathtruncatemacro\number{int(#1*2^0+#2*2^1+#3*2^2)}
}
\newcommand{\sieben}[1]{
    \draw[gray,opacity=.3,line width=3pt] (0,0) --+ (0,.5);
    \draw[gray,opacity=.3,line width=3pt] (0,.6) --+ (0,.5);
    \draw[xshift=.7cm,gray,opacity=.3,line width=3pt] (0,0) --+ (0,.5);
    \draw[xshift=.7cm,gray,opacity=.3,line width=3pt] (0,.6) --+ (0,.5);
    \draw[gray,opacity=.3,line width=3pt] (.1,1.18) --+ (.5,0);
    \draw[gray,opacity=.3,line width=3pt] (.1,-.05) --+ (.5,0);
    \draw[gray,opacity=.3,line width=3pt] (.1,.55) --+ (.5,0);
    \ifthenelse{\equal{#1}{a}}{%\typeout{a}
       \draw[red,line width=3pt] (0,0) --+ (0,.5);
       \draw[red,line width=3pt] (0,.6) --+ (0,.5);
       \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
       \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
       \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
    }{%
      \ifcase#1
          \draw[red,line width=3pt] (0,0) --+ (0,.5);
          \draw[red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
          \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
      \or
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
      \or
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);;
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
          \draw[red,line width=3pt] (0,0) --+ (0,.5);
          \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
      \or
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
          \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
          \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
      \or
          \draw[red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
      \or
          \draw[red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
          \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
          \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
      \or
          \draw[red,line width=3pt] (0,0) --+ (0,.5);
          \draw[red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
          \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
          \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
      \or
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
      \or
          \draw[red,line width=3pt] (0,0) --+ (0,.5);
          \draw[red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
          \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
          \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
      \or
          \draw[red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
          \draw[red,line width=3pt] (.1,-.05) --+ (.5,0);
          \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
      \or
          \draw[red,line width=3pt] (0,0) --+ (0,.5);
          \draw[red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,0) --+ (0,.5);
          \draw[xshift=.7cm,red,line width=3pt] (0,.6) --+ (0,.5);
          \draw[red,line width=3pt] (.1,1.18) --+ (.5,0);
          \draw[red,line width=3pt] (.1,.55) --+ (.5,0);
      \or   
    \fi}}
\begin{document}
    \begin{tikzpicture}[xslant=0]
        \sieben{a}
        \begin{scope}[xshift=1.5cm]
            \binary{1}{1}{1} % seven in dual system
            \sieben{\number}
        \end{scope}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

PSTricks 解决方案仅供娱乐。如果我的算法不够智能,我真的很抱歉,但至少我可以节省更多击键次数。

版本 B

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}
\newif\ifa\newif\ifb\newif\ifc\newif\ifd\newif\ife\newif\iff\newif\ifg
\def\seg#1{%
    \atrue\btrue\ctrue\dtrue\etrue\ftrue\gtrue
    \ifcase#1
        \gfalse%0
    \or
        \afalse\bfalse\cfalse\dfalse\gfalse%1
    \or
        \bfalse\efalse%2
    \or
        \bfalse\cfalse%3
    \or
        \afalse\cfalse\dfalse%4
    \or 
        \cfalse\ffalse%5
    \or
        \ffalse%6
    \or
        \bfalse\cfalse\dfalse\gfalse%7
    \or
        %8
    \or
        \cfalse%9
    \fi
    \begin{pspicture}[nodesep=12pt,linewidth=12pt,linecap=1,linecolor=red](3,6)
        \pnodes{s}(0,0)(3,0)(3,3)(3,6)(0,6)(0,3)
        \ifa\pcline(s3)(s4)\fi
        \ifb\pcline(s4)(s5)\fi
        \ifc\pcline(s5)(s0)\fi
        \ifd\pcline(s0)(s1)\fi
        \ife\pcline(s1)(s2)\fi
        \iff\pcline(s2)(s3)\fi
        \ifg\pcline(s2)(s5)\fi
    \end{pspicture}}
\begin{document}
\foreach\i in {0,1,...,9}{\seg{\i}}
\end{document}

在此处输入图片描述

版本 A

更加紧凑的解决方案。

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}

\def\o#1#2#3{\expandafter\unless\csname if#1\endcsname\pcline(s#2)(s#3)\fi}
\newif\ifa\newif\ifb\newif\ifc\newif\ifd\newif\ife\newif\iff\newif\ifg

\def\p#1{%
    \ifcase#1
        \gtrue%0
    \or
        \atrue\btrue\ctrue\dtrue\gtrue%1
    \or
        \btrue\etrue%2
    \or
        \btrue\ctrue%3
    \or
        \atrue\ctrue\dtrue%4
    \or 
        \ctrue\ftrue%5
    \or
        \ftrue%6
    \or
        \btrue\ctrue\dtrue\gtrue%7
    \or
        %8
    \or
        \ctrue%9
    \fi
    \begin{pspicture}[nodesep=12pt,linewidth=12pt,linecap=1,linecolor=red](3,6)
        \pnodes{s}(0,0)(3,0)(3,3)(3,6)(0,6)(0,3)
        \o{a}{3}{4}
        \o{b}{4}{5}
        \o{c}{5}{0}
        \o{d}{0}{1}
        \o{e}{1}{2}
        \o{f}{2}{3}
        \o{g}{2}{5}
    \end{pspicture}}
\begin{document}
\foreach\i in {0,1,...,9}{\p\i}
\end{document}

答案3

不是 TiZ 答案,但是……几年前我写了一个小程序包(主要是为了好玩),sevseg用来绘制七段数字(和一些其他符号)pict2e。这个程序包不在 CTAN 上,但可以从 GitHub 下载。该存储库还包含一个简短的文档。

\documentclass{article}
\usepackage{sevseg}
\usepackage{xcolor}
\begin{document}
See \sevseg{123}, or \sevseg[bg=black,on=red,off=darkgray,scale=4]{17:59}
\end{document}

在此处输入图片描述

支持从 0 到 9 的所有数字以及从 A 到 F 的字母(大小写无关紧要,输出相同),以及减号、冒号、句号(和逗号,也输出句号):

在此处输入图片描述

不支持的字符会引发警告。

答案4

您可以尝试由 Germain Gondor 编写的这个包。法语和英语文档 https://sciences-indus-cpge.papanicola.info/Afficheur-7-Segments-avec-PGF-TIKZ

相关内容