2

I like to use LaTeX to place some text on top of an existing PDF form, so I don't have to fill it out by hand. For this I'm using microtypes letterspacing feature to stretch the upper-case text across the character boxes of the form. This works well when tt font is used, except if I have a space in the text. Then the space seems to have a shorter width which makes all following characters to be to far to the left so they are touching the box lines, which I try to avoid.

How can I make microtype to make the inter-word spaces inside \textls the same as a normal tt character?

Alternatively, is there a way to have monospaced text while using a different font, like \sffamily?

As this code is a prototype for a form-fillout package I have in mind some manual adjustments like breaking the words appart and placing them separatly is not suitable for me.

Example Form:

See the misplaced second first name (MANFRED) and country name (MUSTERLAND) when there is a space beforehand.

enter image description here

Example Code:
\documentclass[multi=page,border=0,preview]{standalone}
\usepackage{tikz}
\usepackage[tracking=true]{microtype}

\begin{document}
\edef\tempbox{\csname @tempboxa\endcsname}%
\sbox\tempbox{\includegraphics[page=1]{formular.pdf}}%
\begin{page}
\begin{tikzpicture}[x=\wd\tempbox,y=-\ht\tempbox]
\iffalse
\draw [ultra thin,black!10] (0,0) grid [step=2mm] (1, 1);
\draw [very thin,black!50] (0,0) grid [step=1cm] (1, 1);
\pgfmathtruncatemacro\xmax{\wd\tempbox/1cm}
\pgfmathtruncatemacro\ymax{\ht\tempbox/1cm}
\foreach \n in {0,...,\xmax} \node [font=\sffamily\tiny, anchor=north west] at (\n cm, 0) {\n} ;
\foreach \n in {1,...,\ymax} \node [font=\sffamily\tiny, anchor=north west] at (0, -\n cm) {\n} ;
\fi
\node [anchor=north west,inner sep=0pt, outer sep=0pt] {\usebox\tempbox};
%\draw [help lines] (0,0) grid [xstep=0.1\wd\tempbox,ystep=0.1\ht\tempbox] (1, 1);
\useasboundingbox (0,0) rectangle (1,1);

\begin{scope}[x=1cm, y=-1cm,anchor=base west,font=\ttfamily,inner sep=0pt, outer sep=0pt]
\SetTracking{ encoding = *}{ 618 }
\def\placetext#1#2{\node at (#1) {\textls{\MakeUppercase{#2}}};}
\node [inner sep=0pt] at (7.75, 8.825) {$\times$};
%\node [inner sep=0pt] at (8.675, 8.825) {$\times$};
\placetext{2.6, 9.4}{MUSTERMAN}
\placetext{2.6, 10.25}{MAX MANFRED}
\placetext{2.6, 11.1}{MUSTERSTRASSE}
\placetext{16.7, 11.1}{1234}
\placetext{2.6, 11.95}{98765}
\placetext{5.5, 11.95}{MUSTERSTADT, Musterland}
\placetext{2.6, 12.8}{12}
\placetext{3.75, 12.8}{34}
\placetext{4.85, 12.8}{5678}
\placetext{7.15, 12.8}{MUSTERSTADT}

\placetext{2.6, 16.925}{MUSTERSTRASSE}
\placetext{16.7, 16.925}{1234}
\placetext{2.6, 17.85}{98765}
\placetext{5.1725, 17.85}{MUSTERSTADT}
\placetext{2.6, 18.75}{\MakeUppercase{max.mustermann@web.de}}

\node [inner sep=0pt] at (2.615, 19.725) {\Large$\times$};
\end{scope}
\end{tikzpicture}
\end{page}

\end{document}
  • The example cannot be compiled. – egreg Mar 15 '18 at 10:29
  • @egreg: You mean without the PDF form itself, right? Well, I did not want to post it because I don't have the copyright on it. I actually thought the problem is described well enough to get the idea what I want and the example code is there to show my general approach. I didn't had the time yet to make a dummy form myself. – Martin Scharrer Mar 15 '18 at 11:35
5

I added [spacing = {1200*,,}] as optional argument to \SetTracking. For the choice of 1200 I took my lead from @StevenSegletes's answer, after some trial and error with starting point microtype doc example top page 17 MICRO FINE TUNING : Tracking

\documentclass[multi=page,border=0,preview]{standalone}
\usepackage{tikz}
\usepackage[tracking=true]{microtype}

\begin{document}
\edef\tempbox{\csname @tempboxa\endcsname}%
\sbox\tempbox{\fboxsep-\fboxrule\fbox{\vbox to 29.7cm{\hsize21cm\noindent X\dotfill X\par
                    \vfill\noindent X\dotfill X\par}}}%
\begin{page}
\begin{tikzpicture}[x=\wd\tempbox,y=-\ht\tempbox]
\iffalse
\draw [ultra thin,black!10] (0,0) grid [step=2mm] (1, 1);
\draw [very thin,black!50] (0,0) grid [step=1cm] (1, 1);
\pgfmathtruncatemacro\xmax{\wd\tempbox/1cm}
\pgfmathtruncatemacro\ymax{\ht\tempbox/1cm}
\foreach \n in {0,...,\xmax} \node [font=\sffamily\tiny, anchor=north west] at (\n cm, 0) {\n} ;
\foreach \n in {1,...,\ymax} \node [font=\sffamily\tiny, anchor=north west] at (0, -\n cm) {\n} ;
\fi
\node [anchor=north west,inner sep=0pt, outer sep=0pt] {\usebox\tempbox};
%\draw [help lines] (0,0) grid [xstep=0.1\wd\tempbox,ystep=0.1\ht\tempbox] (1, 1);
\useasboundingbox (0,0) rectangle (1,1);

\begin{scope}[x=1cm, y=-1cm,anchor=base west,font=\ttfamily,inner sep=0pt, outer sep=0pt]
\SetTracking[spacing = {1200*,,}]{ encoding = *, }{ 618 }
\def\placetext#1#2{\node at (#1) {\textls{\MakeUppercase{#2}}};}
\node [inner sep=0pt] at (7.75, 8.825) {$\times$};
%\node [inner sep=0pt] at (8.675, 8.825) {$\times$};
\placetext{2.6, 9.4}{MUSTERMAN}
\placetext{2.6, 10.25}{MAX MANFRED}
\placetext{2.6, 11.1}{MUSTERSTRASSE}
\placetext{16.7, 11.1}{1234}
\placetext{2.6, 11.95}{98765}
\placetext{5.5, 11.95}{MUSTERSTADT, Musterland}
\placetext{2.6, 12.8}{12}
\placetext{3.75, 12.8}{34}
\placetext{4.85, 12.8}{5678}
\placetext{7.15, 12.8}{MUSTERSTADT}

\placetext{2.6, 16.925}{MUSTERSTRASSE}
\placetext{16.7, 16.925}{1234}
\placetext{2.6, 17.85}{98765}
\placetext{5.1725, 17.85}{MUSTERSTADT}
\placetext{2.6, 18.75}{\MakeUppercase{max.mustermann@web.de}}

\node [inner sep=0pt] at (2.615, 19.725) {\Large$\times$};
\end{scope}
\end{tikzpicture}
\end{page}

\end{document}

enter image description here

| improve this answer | |
  • Thanks a lot. Looks good within the form. Sorry for not providing it, but it's tricky regarding copyright etc. Anyway, could you just add a short description on top to explain what you changed (so far as I see it, adding [spacing = {1200*,,}] to \SetTracking), so that others with the same problem don't have to compare codes. Thanks. – Martin Scharrer Mar 15 '18 at 11:39
  • yes, sorry will add description. – user4686 Mar 15 '18 at 11:41
3

I took the lead from this question: How to change the interword spacing? and modified \spaceskip, to expand by a factor of 1.2.

\documentclass{article}
\usepackage{tikz}
\usepackage[tracking=true]{microtype}
\def\placetext#1{\textls{\MakeUppercase{#1}}}
\begin{document}

\ttfamily

\placetext{x x x x x x x x x x x x x x x x x x x} (wrong)

\placetext{xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyx}

{\spaceskip=1.2\fontdimen2\font plus 1.2\fontdimen3\font minus 1.2\fontdimen4\font

\placetext{x x x x x x x x x x x x x x x x x x x} (fixed)}

\end{document}

enter image description here

The \spaceskip can be incorporated into the macro definition itself, and grouped to limit its scope:

\def\placetext#1{{%
  \spaceskip=1.2\fontdimen2\font plus 1.2\fontdimen3\font minus 1.2\fontdimen4\font%
  \textls{\MakeUppercase{#1}}%
}}
| improve this answer | |

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.