答案1
这是一个很可爱的问题,尽管我不确定我是否能得到你想要的结果。我决定实现 OP 中的最后一个草图。其他变体很容易修改。
下面的代码定义了一个宏,它接受以逗号分隔的和\BinaryDigits
列表,然后产生如下输出:0
1
以下是代码:
\documentclass{article}
\usepackage{tikz}
\newcommand\BinaryDigits[1]{%
\begin{tikzpicture}[line width=0.8mm,scale=0.5]
\draw(0.5,0)--++(0.5,0);
\foreach \d [count=\c, remember=\c as \C] in {#1} {
\ifnum\d=1 \draw(\c,0) rectangle ++(1,1);
\else \draw(\c,0) rectangle ++(1,-1);
\fi
\node at (\c+0.5,1.5){\d};
}
\draw(\C+1,0)--+(0.5,0);
\end{tikzpicture}%
}
\begin{document}
\BinaryDigits{0,1,1,0,1,0,1,1,1}
\BinaryDigits{0,0,0,1,0,0,1,0,0,0,0,1,1,0}
\end{document}
编辑
这是仅绘制路径的变体。这很有趣,因为要使路径连续,最简单的方法是将其放在命令\foreach
内部\draw
:
代码如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\BinaryDigits[1]{%
\begin{tikzpicture}[line width=0.8mm,scale=0.5]
\draw(0.5,0)--++(0.5,0)
\foreach \d [count=\c, remember=\c as \C, evaluate=\d as \D using {int(2*\d-1)}] in {#1}
{ --(\c,\D)--++(1,0) } --(\C+1,0)--++(0.5,0);
\foreach \d [count=\c] in {#1} {\node at (\c+0.5,0){$\d$};}
\end{tikzpicture}
}
\begin{document}
\BinaryDigits{0,1,1,0,1,0,1,1,1}
\bigskip
\BinaryDigits{0,0,0,1,0,0,1,0,0,0,0,1,1,0}
\end{document}