# How to fit characters (numbers) into a graph paper?

I want to print some calculations into a graph paper (just some easy calculations like adding some numbers...).

An (ugly) example with a tabular:

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


There are some solutions to print a graph paper/squared paper:

So I tried to find a solution with a graph paper, but I have problems to write the text into the squared boxes.

My solution up to now:

\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}
}
\node[anchor=west] at (0,0) [yshift=\value{gridypos}*#1-.5*#1]{##2};  %fixme one box per character(including spaces)
}
%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}


The result:

There are two missing things:

• The macro \gridtext should get a 2nd parameter to define the x-pos (I think that's something I can make on my own if my other problem is solved)
• The text does not fit into the squared boxes (one character (number, operator or space) per box)

There are some ideas how I can do it, but I have no idea, how to do it:

• parse the parameter and position character by character into its box.
• Use a monospace font with the exact length of the optional parameter of my squaredpaper-environment.

Remarks:

• The spaces in the \gridtext parameter should be respected (and if this is to difficult I could use ~ as placeholder.)
• Up to now I don't want to add a text, but maybe a *-variant of \gridtext to add text would be fine (but I think, that's something I could do on my own if I need it).
• Do you want one character per box, two, or what exactly? – Steven B. Segletes Feb 18 '15 at 19:15
• One character (number, space or operator) per box. – knut Feb 18 '15 at 19:19
• You realize that the font will not be "naturally" spaced for that solution, since you are, in essence forcing the height allocated to a character to equal the width allocated for the character. – Steven B. Segletes Feb 18 '15 at 19:22
• Yes, that's ok. It would be ok to use a monospaced font, but I need a specific size for it. -- Hmm, one idea as solution: Use a monospaced font and set the box with to 1em of this font? – knut Feb 18 '15 at 19:28

I did add spaces manually to the input by way of ~. While the macro could be written to intercept a single space in the input between characters, all of LaTeX's parsing mechanisms short of verbatim will read multiple spaces as a single space. Therefore, using \ or ~ makes sense as the right approach for hard spacing.

The result of \gridtext is achieved by parsing the input one character at a time, and setting it in a box the size of the unit-grid cell.

The macro \gridtext now has a 2nd mandatory argument that specifies the horizontal offset in squares.

EDITED to us \obeyspaces, rather than requiring the use of hard spaces in the \gridtext argument.

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


If the size is changed to \scriptsize, it still works.

If the grid size is changed (e.g., \begin{squaredpaper}[.4cm]{19}), it still works:

• Great! Thanks. I will use it with ~ - so I can align the output in my editor. I think I will accept your answer after a latency to give the possibility for other answers. – knut Feb 18 '15 at 19:49