Two macros for a digital display with seven segments. One is for coding hexadecimal numbers from 0 to F. The other one allows switching on and off arbitrary segments.
Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the
LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.
En français: TeXnique.fr.
% A seven segment digital display
% Author: Uwe Zimmermann
\documentclass[a5paper]{article}
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfkeys{
/sevenseg/.is family, /sevenseg,
slant/.estore in = \sevensegSlant, % vertical slant in degrees
size/.estore in = \sevensegSize, % length of a segment
shrink/.estore in = \sevensegShrink, % avoids overlapping of segments
line width/.estore in = \sevensegLinewidth, % thickness of the segments
line cap/.estore in = \sevensegLinecap, % end cap style rect, round, butt
oncolor/.estore in = \sevensegOncolor, % color of an ON segment
offcolor/.estore in = \sevensegOffcolor, % color of an OFF segment
}
\pgfkeys{
/sevenseg,
default/.style = {slant = 0, size = 1em, shrink = 0.1,
line width = 0.15em, line cap = butt,
oncolor = red, offcolor = gray}
}
%===============================================
% a b c d e f g - segment values
% \sevenseg[options]{{1,1,1,1,1,1,0,}}
%
\newcommand{\sevenseg}[2][]% options values
{%
\pgfkeys{/sevenseg, default, #1}%
\def\sevensegarray{#2}%
\begin{tikzpicture}%
% first define the position of the 6 corner points
\path (0,0) ++(0,0) coordinate (P1);
\path (0,0) ++(\sevensegSize,0) coordinate (P2);
\path (0,0) ++(90-\sevensegSlant:\sevensegSize) coordinate (P3);
\path (P2) ++(90-\sevensegSlant:\sevensegSize) coordinate (P4);
\path (P3) ++(90-\sevensegSlant:\sevensegSize) coordinate (P5);
\path (P4) ++(90-\sevensegSlant:\sevensegSize) coordinate (P6);
% then step through the 1/0 values in the segment array
\foreach \i in {0,...,6}%
{
\pgfmathparse{\sevensegarray[\i]}
\ifthenelse{\equal{\pgfmathresult}{1}}%
{\let\mycolor=\sevensegOncolor}% segment is on
{\let\mycolor=\sevensegOffcolor}% segment is off
\tikzstyle{segstyle} = [draw=\mycolor, line width = \sevensegLinewidth,
line cap = \sevensegLinecap]
%-----------------------
\ifthenelse{\equal{\i}{0}}{\path[segstyle]
(${1-\sevensegShrink}*(P5)+\sevensegShrink*(P6)$)
-- ($\sevensegShrink*(P5)+{1-\sevensegShrink}*(P6)$);}{} % a
\ifthenelse{\equal{\i}{1}}{\path[segstyle]
(${1-\sevensegShrink}*(P6)+\sevensegShrink*(P4)$)
-- ($\sevensegShrink*(P6)+{1-\sevensegShrink}*(P4)$);}{} % b
\ifthenelse{\equal{\i}{2}}{\path[segstyle]
(${1-\sevensegShrink}*(P4)+\sevensegShrink*(P2)$)
-- ($\sevensegShrink*(P4)+{1-\sevensegShrink}*(P2)$);}{} % c
\ifthenelse{\equal{\i}{3}}{\path[segstyle]
(${1-\sevensegShrink}*(P1)+\sevensegShrink*(P2)$)
-- ($\sevensegShrink*(P1)+{1-\sevensegShrink}*(P2)$);}{} % d
\ifthenelse{\equal{\i}{4}}{\path[segstyle]
(${1-\sevensegShrink}*(P1)+\sevensegShrink*(P3)$)
-- ($\sevensegShrink*(P1)+{1-\sevensegShrink}*(P3)$);}{} % e
\ifthenelse{\equal{\i}{5}}{\path[segstyle]
(${1-\sevensegShrink}*(P3)+\sevensegShrink*(P5)$)
-- ($\sevensegShrink*(P3)+{1-\sevensegShrink}*(P5)$);}{} % f
\ifthenelse{\equal{\i}{6}}{\path[segstyle]
(${1-\sevensegShrink}*(P3)+\sevensegShrink*(P4)$)
-- ($\sevensegShrink*(P3)+{1-\sevensegShrink}*(P4)$);}{} % g
}
\end{tikzpicture}%
}
\newcommand{\sevensegnum}[2][]% sample characvters
{%
\ifthenelse{\equal{#2}{0}}{\sevenseg[#1]{{1,1,1,1,1,1,0,}}}{%
\ifthenelse{\equal{#2}{1}}{\sevenseg[#1]{{0,1,1,0,0,0,0,}}}{%
\ifthenelse{\equal{#2}{2}}{\sevenseg[#1]{{1,1,0,1,1,0,1,}}}{%
\ifthenelse{\equal{#2}{3}}{\sevenseg[#1]{{1,1,1,1,0,0,1,}}}{%
\ifthenelse{\equal{#2}{4}}{\sevenseg[#1]{{0,1,1,0,0,1,1,}}}{%
\ifthenelse{\equal{#2}{5}}{\sevenseg[#1]{{1,0,1,1,0,1,1,}}}{%
\ifthenelse{\equal{#2}{6}}{\sevenseg[#1]{{1,0,1,1,1,1,1,}}}{%
\ifthenelse{\equal{#2}{7}}{\sevenseg[#1]{{1,1,1,0,0,0,0,}}}{%
\ifthenelse{\equal{#2}{8}}{\sevenseg[#1]{{1,1,1,1,1,1,1,}}}{%
\ifthenelse{\equal{#2}{9}}{\sevenseg[#1]{{1,1,1,1,0,1,1,}}}{%
\ifthenelse{\equal{#2}{A}}{\sevenseg[#1]{{1,1,1,0,1,1,1,}}}{%
\ifthenelse{\equal{#2}{B}}{\sevenseg[#1]{{0,0,1,1,1,1,1,}}}{%
\ifthenelse{\equal{#2}{C}}{\sevenseg[#1]{{0,0,0,1,1,0,1,}}}{%
\ifthenelse{\equal{#2}{D}}{\sevenseg[#1]{{0,1,1,1,1,0,1,}}}{%
\ifthenelse{\equal{#2}{E}}{\sevenseg[#1]{{1,0,0,1,1,1,1,}}}{%
\ifthenelse{\equal{#2}{F}}{\sevenseg[#1]{{1,0,0,0,1,1,1,}}}{%
{\sevenseg[#1]{{0,0,0,0,0,0,0,}}}}}}}}}}}}}}}}}}}%
}
\begin{document}
\begin{center}
\foreach \n in {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,' '}%
{\sevensegnum[offcolor=white]{\n} }
\bigskip
\sevenseg[size = 2em, line cap = butt]{{0,1,1,0,0,0,0,}}
\sevenseg[size = 2em, line cap = round]{{1,1,0,1,1,0,1,}}
\sevenseg[size = 2em, line cap = rect]{{1,1,1,1,1,1,1,}}
\sevenseg[size = 2em, slant = 5, shrink = 0, line cap = round,
oncolor = green]{{1,1,1,1,0,0,1,}}
\bigskip
\sevenseg{{1,1,1,1,1,1,1,}}
\tikz{\node[inner sep = 0pt, draw = blue] {\sevensegnum{8}}}
\tikz{\node[inner sep = 0pt, draw = blue] {\sevensegnum[slant = 20]{8}}}
\end{center}
\end{document}
Comments
Adding comments is currently not enabled.