我正在创建一个带编号的方框形式,用于有限字写作练习,其中一个单元代表一个字符。每个单元都有一个上框和下框。上框将容纳字符,下框将容纳代表字符的数字。一个单元如下所示:
目前表单的字符限制为 80 个字符。到目前为止,我已经手动制作了 12 个框,这很乏味。这是 100% 缩放时的输出:
以下是 MWE:
\documentclass[a4paper]{scrbook}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzstyle{box_upper}=[rectangle, draw, anchor=south, text centered, minimum width=8mm, minimum height=8mm]%
\tikzstyle{box_lower}=[rectangle, draw, anchor=north west, text centered, minimum width=8mm, minimum height=4mm]%
\begin{tikzpicture}
\node[box_lower] (a) at (0,0) {1};
\node[box_upper] (x1) at (a.north) {};
%
\node[box_lower] (b) at (a.north east) {2};
\node[box_upper] (x2) at (b.north) {};
%
\node[box_lower] (c) at (b.north east) {3};
\node[box_upper] (x3) at (c.north) {};
%
\node[box_lower] (d) at (c.north east) {4};
\node[box_upper] (x4) at (d.north) {};
%
\node[box_lower] (e) at (d.north east) {5};
\node[box_upper] (x5) at (e.north) {};
%
\node[box_lower] (f) at (e.north east) {6};
\node[box_upper] (x6) at (f.north) {};
%
\node[box_lower] (g) at (f.north east) {7};
\node[box_upper] (x7) at (g.north) {};
%
\node[box_lower] (h) at (g.north east) {8};
\node[box_upper] (x8) at (h.north) {};
%
\node[box_lower] (i) at (h.north east) {9};
\node[box_upper] (x9) at (i.north) {};
%
\node[box_lower] (j) at (i.north east) {10};
\node[box_upper] (x10) at (j.north) {};
%
\node[box_lower] (k) at (0,-2) {11};
\node[box_upper] (x11) at (k.north) {};
%
\node[box_lower] (l) at (k.north east) {12};
\node[box_upper] (x12) at (l.north) {};
\end{tikzpicture}
\end{document}
我的问题是:
出于好奇,这是正确的方法吗?还有其他方法可以达到相同的效果吗?
有人能制作一个自动化版本吗?每 20 个框后换行一次?可能吗?
将来我计划重新使用这个方框形式作为手写练习表,方法是:
a. 增大上部盒子的宽度和高度。
b. 增加字符限制,比如说 300 个字符。
c. 第 10 个框后分行。
如果可能的话,这些可改变的因素是否也可以包含在自动化版本中?
答案1
您可以使用循环。
\documentclass[a4paper]{scrbook}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\totalbox{80}
\def\breakat{20}
\def\boxheight{.7cm}
\def\boxwidth{.7cm}
\def\numheight{.4cm}
\tikzset{
box/.style={
inner sep=0pt,draw,minimum height=\boxheight,
minimum width=\boxwidth
},
num/.style={
draw,font=\sffamily\scriptsize,minimum width=\boxwidth,
inner sep=0pt,minimum height=\numheight
}
}
\foreach \i [parse=true] in {1,...,\totalbox/\breakat} {% thanks to Schrodinger's cat!
\coordinate (\i-0) at (0,-\i*\boxheight*2.2);
\foreach \j [count=\k from 0] in {1,...,\breakat} {
\path (\i-\k.east) node[right,box] (\i-\j) {};
\pgfmathtruncatemacro\output{\breakat*(\i-1)+\j}
\path (\i-\j.south) node[below,num] {\output};
}
}
\end{tikzpicture}
\end{document}
回答您的问题:
- 不,我认为使用循环(
pgffor
)是正确的方法。 - 嗯,见上文。它是自动化的。
\totalbox
更改、\breakat
、\boxheight
和的值\boxwidth
以\numwidth
查看变化。
一些说明:
- 不要使用
\tikzstyle
。它已被弃用。请改用\tikzset
,就像我在上面的代码中所做的那样。
答案2
概念上类似于Fractal 的答案因为它使用循环。这里是一个循环并使用mod
。这定义了一个图片,允许您设置n
框的数量和pr
每行的框数量。
\documentclass[a4paper]{scrbook}
\usepackage{tikz}
\begin{document}
\tikzset{box_upper/.style={rectangle, draw, anchor=south, text centered, minimum
width=8mm, minimum height=8mm},
box_lower/.style={rectangle, draw, anchor=north west, text centered, minimum
width=8mm, minimum height=4mm},
pics/numbered box/.style={code={
\tikzset{numbered box/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/numbered box/##1}}
\foreach \X in {1,...,\pv{n}}
{\pgfmathtruncatemacro{\Z}{Mod(\X,\pv{pr})}
\pgfmathtruncatemacro{\Y}{-2*int((\X-1)/\pv{pr})}
\ifnum\Z=1
\node[box_lower] (y\X) at (0,\Y) {\X};
\else
\node[box_lower] (y\X) at (y\the\numexpr\X-1\relax.north east) {\X};
\fi
\node[box_upper] (x\X) at (y\X.north) {};}}},
numbered box/.cd,n/.initial=20,pr/.initial=10
}%
\noindent
\begin{tikzpicture}
\pic{numbered box={n=30,pr=18}};
\end{tikzpicture}
\bigskip\bigskip\bigskip\bigskip
\noindent
\begin{tikzpicture}
\pic{numbered box={n=50,pr=15}};
\end{tikzpicture}
\end{document}
如果希望每行有 20 个框,则需要使页面更宽或者使框更小,以避免出现水平框过满的警告。
\documentclass[a4paper]{scrbook}
\usepackage{tikz}
\begin{document}
\tikzset{box_upper/.style={rectangle, draw, anchor=south, text centered, minimum
width=8mm, minimum height=8mm},
box_lower/.style={rectangle, draw, anchor=north west, text centered, minimum
width=8mm, minimum height=4mm},
pics/numbered box/.style={code={
\tikzset{numbered box/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/numbered box/##1}}
\foreach \X in {1,...,\pv{n}}
{\pgfmathtruncatemacro{\Z}{Mod(\X,\pv{pr})}
\pgfmathtruncatemacro{\Y}{-2*int((\X-1)/\pv{pr})}
\ifnum\Z=1
\node[box_lower] (y\X) at (0,\Y) {\X};
\else
\node[box_lower] (y\X) at (y\the\numexpr\X-1\relax.north east) {\X};
\fi
\node[box_upper] (x\X) at (y\X.north) {};}}},
numbered box/.cd,n/.initial=20,pr/.initial=10
}%
\noindent
\begin{tikzpicture}[scale=0.9,transform shape]
\pic{numbered box={n=30,pr=20}};
\end{tikzpicture}
\end{document}