如何將字元(數字)放入方格紙中?

如何將字元(數字)放入方格紙中?

我想將一些計算列印到方格紙上(只是一些簡單的計算,例如添加一些數字...)。

一個帶有表格的(醜陋的)範例:

\begin{tabular}{|*{10}{r|}}\hline
 &   &   &   &  &   &   & &  &  \\\hline
 &   & 1 & + &  & 1 & = & &  & 2\\\hline
 & 1 & 1 & + &  & 1 & = & &  1 & 2\\\hline
 & 1 & 1 & + & 1& 1 & = & &  2 & 2\\\hline
 &   &   &   &  &   &   & &  &  \\\hline
\end{tabular}

在此輸入影像描述

列印方格紙/方格紙有一些解決方案:

所以我嘗試用方格紙找到解決方案,但將文字寫入方格時遇到問題。

到目前為止我的解決方案:

\documentclass[12pt,a6paper]{scrartcl} 
\usepackage{tikz}
\usepackage{ifthen}

\newcounter{gridypos}
\newenvironment{squaredpaper}[2][0.5cm]{% 
  \setcounter{gridypos}{#2}
  \newcommand\gridtext[2][\relax]{
    \ifthenelse{\equal{##1}{\relax}}{}{
         \setcounter{gridypos}{#2}
        \addtocounter{gridypos}{-##1}
        \addtocounter{gridypos}{1}
    }
    \node[anchor=west] at (0,0) [yshift=\value{gridypos}*#1-.5*#1]{##2};  %fixme one box per character(including spaces)
    \addtocounter{gridypos}{-1}%Next \gridtext one line lower
  }
  %Make the grid
  %Source: http://texwelt.de/wissen/fragen/2639/wie-kann-ich-kastchenpapier-zeichnen
  \begin{tikzpicture}[gray,step=#1]
     \pgfmathtruncatemacro\anzahl{(\linewidth-\pgflinewidth)/#1} % maximale Anzahl Kaestchen pro Zeile
     \draw (0,0) rectangle (\anzahl*#1,#2*#1) (0,0) grid (\anzahl*#1,#2*#1);
  }{
   \end{tikzpicture} 
}

\begin{document} 
  \begin{squaredpaper}{20}
    %Start on top line and go down.
    \gridtext{ 1+ 1= 2}
    \gridtext{ 1+ 2= 3}
    \gridtext{ 1+ 3= 4}
    \gridtext{ 1+10=11}
    %Make fix positions
    \gridtext[10]{10+10=20}  %Start in line 10
    \gridtext[15]{15+10=25}  %Start in line 15
    \gridtext[18]{18+10=28}  %Start in line 18
  \end{squaredpaper}

\end{document}

結果: 在此輸入影像描述

缺少兩件事:

  • 巨集 \gridtext 應該獲得第二個參數來定義 x-pos (我認為如果我的其他問題解決,我可以自己做一些事情)
  • 文字無法放入方框(每個方框一個字元(數字、運算符或空格))

我有一些想法如何做到這一點,但我不知道如何做到:

  • 將參數和位置逐個字元解析到其框中。
  • 使用等寬字體和我的 squaredpaper 環境的可選參數的精確長度。

評論:

  • 應該尊重 \gridtext 參數中的空格(如果這很困難,我可以使用 ~ 作為佔位符。)
  • 到目前為止,我不想添加文本,但也許使用 \gridtext 的 * 變體來添加文本就可以了(但我認為,如果需要的話,我可以自己做)。

答案1

我確實通過~.雖然可以編寫巨集來截取字元之間輸入中的單一空格,但所有 LaTeX 的解析機制verbatim都會將多個空格讀取為單一空格。因此,使用\or~作為硬間距的正確方法是有意義的。

的結果\gridtext是透過一次解析輸入一個字元並將其設置在單位網格單元大小的框中來實現的。

該巨集\gridtext現在有第二個強制參數,用於指定正方形中的水平偏移。

已編輯給我們\obeyspaces,而不是要求在論證中使用硬空格\gridtext

\documentclass[12pt,a6paper]{scrartcl} 
\usepackage{tikz}
\usepackage{ifthen}

\newcounter{gridypos}
\newenvironment{squaredpaper}[2][.5cm]{\obeyspaces% 
  \setcounter{gridypos}{#2}
  \newcommand\gridtext[3][\relax]{
    \ifthenelse{\equal{##1}{\relax}}{}{
         \setcounter{gridypos}{#2}
        \addtocounter{gridypos}{-##1}
        \addtocounter{gridypos}{1}
    }
%\tmp@dim0=#1\relax%
    \node[anchor=west] at (-.15,0) [yshift=\value{gridypos}*#1-.5*#1]{%
      \hspace{##2\dimexpr#1\relax}\spaceout{#1}{##3}};  %fixme one box per character(including spaces)
    \addtocounter{gridypos}{-1}%Next \gridtext one line lower
  }
  %Make the grid
  %Source: http://texwelt.de/wissen/fragen/2639/wie-kann-ich-kastchenpapier-zeichnen
  \begin{tikzpicture}[gray,step=#1]
     \pgfmathtruncatemacro\anzahl{(\linewidth-\pgflinewidth)/#1} % maximale Anzahl Kaestchen pro Zeile
     \draw (0,0) rectangle (\anzahl*#1,#2*#1) (0,0) grid (\anzahl*#1,#2*#1);
  }{
   \end{tikzpicture} 
}
\newcommand\spaceout[2]{\def\charwd{#1}\spaceouthelp#2\relax\relax\relax}
\def\spaceouthelp#1#2\relax{%
  \ifx#1\relax\else%
    \makebox[\charwd]{#1}%
    \spaceouthelp#2\relax%
  \fi%
}

\begin{document} 
\ttfamily%\scriptsize% WILL WORK IF UNCOMMENTED
  \begin{squaredpaper}{19}
    %Start on top line and go down.
    \gridtext{0}{1+ 1= 2}
    \gridtext{0}{1+ 2= 3}
    \gridtext{0}{1+ 3= 4}
    \gridtext{0}{1+10=11}
    %Make fix positions
    \gridtext[10]{3}{10+10=20}  %Start in line 10
    \gridtext[15]{4}{15+10=25}  %Start in line 15
    \gridtext[18]{5}{18+10=28}  %Start in line 18
  \end{squaredpaper}

\end{document}

在此輸入影像描述

如果大小更改為\scriptsize,它仍然有效。

在此輸入影像描述

如果網格大小改變(例如\begin{squaredpaper}[.4cm]{19}),它仍然有效:

在此輸入影像描述

相關內容