4
\$\begingroup\$

I made myself a smallish template for practice writing some characters, see below. The objective for me was to have an easy way to enter a couple of characters, set the parameters (how many pre-drawn characters, how many columns, etc.) and then get a print-ready PDF.

example PDF output

It's written against TikZ and LuaLaTeX (I'm not sure it will run with other configurations), but I'm only using TikZ features, no Lua (yet).

I'm looking for feedback especially regarding maintainability, feature configuration (at the moment I'm simply editing the file, but it's not really convenient; I'm thinking more of how to do this easily from the command line?) and ease of use (the dimensions are manually specified - how should I structure this to have it usable for a multi-page setup without too much hassle?) - I've also been thinking of how to drive this from a web service, passing in options file paper size etc. since not everyone might use A4.

What's not added yet is settings with respect to. the drawing style (I'd possibly like to toggle some of the background grid lines) — are there established conventions how to do the background grid, not manually, basically? Note that I'm overdrawing significantly in order to keep the logic simple.

Small edit: I'd be especially grateful for advice explaining how to get rid of the horrible equations — defining new names for simply subtracting a number is, uh, awful.

\documentclass[a4paper]{article}
\usepackage[cm]{fullpage}
\usepackage{fontspec}
\usepackage[utf8]{luainputenc}
\usepackage{fontenc}
\usepackage{lmodern}
\usepackage[german]{babel}
\usepackage{luatexja-fontspec}
% https://ctan.org/tex-archive/fonts/fandol?lang=en
\setmainjfont{FandolKai}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{xstring}

\begin{document}

\def\c{gray!50}
\def\chars{我,你,您,也,很,他,们,好,吗,再,见,是,累,俄,渴,不,谢,用,爱,咖,啡,这,欧,雅,福}
\def\w{17}
\def\wFull{1}
\def\wHalf{2}

\StrCount{\chars}{,}[\h]
\pgfmathsetmacro{\h}{\h+1}
\pgfmathsetmacro{\hMinusOne}{\h-1}

\pgfmathsetmacro{\wMinusOne}{\w-1}
\pgfmathsetmacro{\wFullMinusOne}{\wFull+1}
\pgfmathsetmacro{\wAll}{\wFull+\wHalf}
\pgfmathsetmacro{\wPlusH}{\w+\h}

\begin{center}
\begin{tikzpicture}

\draw[step=1cm,dashed,gray,thin] (0,0) grid (\w,\h);

\foreach \x in {0,...,\wMinusOne}
{
  \pgfmathsetmacro{\a}{\x+0.5}
  \draw[dashed,\c,very thin] (\a,0) -- (\a,\h);
}
\foreach \y in {0,...,\hMinusOne}
{
  \pgfmathsetmacro{\a}{\y+0.5}
  \draw[dashed,\c,very thin] (0,\a) -- (\w,\a);
}

\begin{scope}

\clip(0,0) rectangle(\w,\h);

\foreach \x in {1,...,\wPlusH}
{
  \pgfmathsetmacro{\xh}{\x-\h}
  \draw[dashed,\c,very thin] (\xh,\h) -- (\x,0);
}

\foreach \x in {0,...,\wPlusH}
{
  \pgfmathsetmacro{\xMinusH}{\x-\h}
  \pgfmathsetmacro{\xh}{\x+\h}
  \draw[dashed,\c,very thin] (\xMinusH,0) -- (\x,\h);
}

\end{scope}

\newcounter{counter}\setcounter{counter}{0}

\foreach \char in \chars
{

\pgfmathsetmacro{\y}{\h-1-\thecounter+0.55}

\foreach \x in {0,...,\wFullMinusOne}
{
  \pgfmathsetmacro{\xo}{\x+0.5}
  \node at (\xo,\y) {\huge\char};
}

\foreach \x in {\wFull,...,\wAll}
{
  \pgfmathsetmacro{\xo}{\x+0.5}
  \node[gray] at (\xo,\y) {\huge\char};
}

\stepcounter{counter}

}

\end{tikzpicture}
\end{center}

\end{document}
\$\endgroup\$
  • \$\begingroup\$ First thing without testing the code: remove the call to luainputenc. \$\endgroup\$ – egreg Mar 8 at 22:17
4
+50
\$\begingroup\$

You can just start your loops from 1 to \w, instead of 0 and ending to \wMinusOne: just subtract 0.5, instead of adding it.

Using \c for a color is not the best idea: properly define a color, here I used the name grid.

Also the counter is not necessary, as there's a \foreach feature for it.

I also put all \def inside tikzpicture and changed \char into \character, as \char is a TeX primitive connected to character printing and there's high risk of conflict.

Don't use luainputenc: it is aimed at compatibility of older documents. Also loading fontenc and lmodern is useless.

\documentclass[a4paper]{article}
\usepackage[cm]{fullpage}
\usepackage{fontspec}
\usepackage[german]{babel}
\usepackage{luatexja-fontspec}
% https://ctan.org/tex-archive/fonts/fandol?lang=en
\setmainjfont{FandolKai}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{xstring}

\begin{document}

\begin{center}
\begin{tikzpicture}

\colorlet{grid}{gray!50}
\def\chars{我,你,您,也,很,他,们,好,吗,再,见,是,累,俄,渴,不,谢,用,爱,咖,啡,这,欧,雅,福}
\def\w{17}
\def\wFull{1}
\def\wHalf{2}

\StrCount{\chars}{,}[\h]
\pgfmathsetmacro{\h}{\h+1}

\pgfmathsetmacro{\wAll}{\wFull+\wHalf}
\pgfmathsetmacro{\wPlusH}{\w+\h}


\draw[step=1cm,dashed,gray,thin] (0,0) grid (\w,\h);

\foreach \x in {1,...,\w}
 {
  \pgfmathsetmacro{\a}{\x-0.5}
  \draw[dashed,grid,very thin] (\a,0) -- (\a,\h);
 }
\foreach \y in {0,...,\h}
 {
  \pgfmathsetmacro{\a}{\y-0.5}
  \draw[dashed,grid,very thin] (0,\a) -- (\w,\a);
 }

\begin{scope}

\clip(0,0) rectangle(\w,\h);

\foreach \x in {1,...,\wPlusH}
 {
  \pgfmathsetmacro{\xh}{\x-\h}
  \draw[dashed,grid,very thin] (\xh,\h) -- (\x,0);
 }

\foreach \x in {0,...,\wPlusH}
 {
  \pgfmathsetmacro{\xMinusH}{\x-\h}
  \pgfmathsetmacro{\xh}{\x+\h}
  \draw[dashed,grid,very thin] (\xMinusH,0) -- (\x,\h);
 }

\end{scope}

\foreach \character [count=\charcount from 0] in \chars 
 {
  \pgfmathsetmacro{\y}{\h-1-\charcount+0.55}
  \foreach \x in {1,...,\wFull}
   {
    \pgfmathsetmacro{\xo}{\x-0.5}
    \node at (\xo,\y) {\huge\character};
   }
  \foreach \x in {\wFull,...,\wAll}
   {
    \pgfmathsetmacro{\xo}{\x+0.5}
    \node[gray] at (\xo,\y) {\huge\character};
   }
 }

\end{tikzpicture}
\end{center}

\end{document}

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Thanks, I'd hoped to get this smaller, I suppose another option would be to go via Lua then? Any suggestion about the background or how to apply this to multiple pages, or is this reasonable to do as it is? \$\endgroup\$ – ferada Mar 20 at 16:03
  • \$\begingroup\$ @ferada I can't see how to shorten it. \$\endgroup\$ – egreg Mar 20 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.