6

Consider the following 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}

Here is the output:

Screenshot

My problem is: When I uncomment the commented part in the definition of the command (\ifnum#1>9 …) and I type as an argument the letter "a", I get an error. But when I comment this part, I don't.

My question is: How can I fix this error?

3
  • 1
    a is not a number, you should use something like \sieben{1}. If you want the argument to be a hexadecimal you should use \ifnum"#1 and \sieben{A}. – Ulrike Fischer Jan 6 '19 at 17:39
  • 1
    How about \ifcase#1? Happy new year! – user121799 Jan 6 '19 at 19:26
  • 1
    @marmot: This was exactly that what I was looking for. Thank you and a happy new year! – current_user Jan 6 '19 at 20:15
7

Most likely I do not understand your question. But I'd like to argue that one should first check if the argument is a number (I only check if it is an a but this could be extended) and then use \ifcase for the integer cases.

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

enter image description here

3
  • I meant that when I have a number which is larger then 9 I get the "E" (for error). – current_user Jan 7 '19 at 21:04
  • 1
    @current_user I just tried ` \sieben{10}` and it worked. What did you try? – user121799 Jan 7 '19 at 21:23
  • Now it works. This is funny, I promise I tried exactly this code and it didn't work. Now it works. I just looked up at my tmp folder and it was the exact same code and didn't work (also checked up the log file) … – current_user Jan 8 '19 at 19:15
8

A PSTricks solution only for fun purposes. I am really sorry if my algorithm is not so smart, but at least I can save more keystrokes.

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

enter image description here

Version A

A more compact solution.

\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
  • 2
    I wonder which one saves more keystrokes: (A) letting the default value of newly created conditional macros be false and turn them true or (B or the current algorithm) in which the newly created conditional macros are set true first and change them to false later on. – Kim Jong Un Jan 6 '19 at 22:17
  • 2
    The ifthen tricks are very elegant. +1 – user121799 Jan 6 '19 at 23:27
  • 1
    This is for me the simplest solution. +1! – current_user Jan 7 '19 at 21:03
1

It's a bit of a cheat (and I guess technically not answering the question), but you could download the fonts from the DSEG project put one of them (in this case I used DSEG7Classic-Regular.ttf) the in the same folder as your tex source file and compile the following with LuaLaTeX or XeLaTeX:

\RequirePackage{luatex85} % Not required for XeLaTeX
\documentclass[border=5]{standalone}
\usepackage{fontspec,xcolor}
\newfontfamily{\dseg}[Path=./]{DSEG7Classic-Regular}
\def\display#1{\setbox0=\hbox{\textcolor{on}{\dseg#1}}%
\hbox to\wd0{\textcolor{off}{\dseg\leaders\hbox{8}\hfill\null}}%
\kern-\wd0\box0}
\colorlet{on}{red}
\colorlet{off}{gray!10}
\begin{document}
\display{0123456789abcdef}
\end{document}

enter image description here

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.