1

The following text uses PGF/TikZ to write on top of a PDF file. The use case is filling a PDF form.

In some cases, one wants to write a block of text which is aligned. Let's say left aligned, as in the example below.

However, in addition one might want the lines in that block of text to be arbitrarily spaced. I'm not sure of a good way to this, so that is my question.

How can I write a block of left aligned text that has arbitrarily specified spacing, including the possibility of non-uniform spacing?

The code I'm using writes the block of text inside a node. I don't have to do it that way, but that's the way I've been doing is so far. If I put the lines in separate nodes, then I would have to align them manually.

\documentclass[twoside=semi, 12pt]{scrartcl}
\usepackage{tikz}
\usepackage{pdfpages}
\usepackage[T1]{fontenc}
\usepackage{fouriernc}
% From https://unix.stackexchange.com/q/277892/4671                                                                            
% echo | groff -T pdf > blank.pdf                                                                                              
% echo "" | ps2pdf -sPAPERSIZE=a4 - blank.pdf                                                                                  
\def\Filename{blank.pdf}

%http://www.freepubquiz.co.uk/famous-addresses.html
\begin{document} \includepdf[pagecommand=
{\begin{tikzpicture}[remember picture, overlay]
\node [font=\fontsize{11}{11}\bfseries, align=left] at (11, -1.5){Dr John Dolittle\Oxenthorpe Road\Puddleby-on-the-Marsh
\Slopshire, England};
\end{tikzpicture}}
,pages=1]{\Filename} \end{document}

Faheem Mitha
  • 7,778
  • if you give a tikz node a width it's a minipage so can have multi-line text (but you wouldn't want \fontsize{11}{11} for multiline text) – David Carlisle Nov 21 '21 at 12:24
  • Alternatively, would something like \matrix [matrix of nodes,nodes={anchor=base west, font=\bfseries\strut}, row sep={15pt,between origins}] {Dr John Dolittle\\Oxenthorpe Road\\Puddleby-on-the-Marsh\\Slopshire, England\\}; with \usetikzlibrary{matrix} work? Modify the 15pt to whatever you prefer. – Torbjørn T. Nov 21 '21 at 12:34
  • @TorbjørnT. If it's not clear from my question, I didn't want to restrict myself to uniform separation. Should I have been more explicit? – Faheem Mitha Nov 21 '21 at 12:44
  • Clearly it wasn't clear to me at least :) Similar to in normal text you can specify spacing on a per-line basis with e.g. \\[2cm]. – Torbjørn T. Nov 21 '21 at 12:48
  • \\[2cm] indeed works, though I'm not familiar with that syntax. I tried \vspace, but that does not work correctly in horizontal mode - the space is inserted after the line containing the \vspace command. – Faheem Mitha Nov 21 '21 at 12:58
  • It looks like https://tex.stackexchange.com/q/198299/3406 discusses this usage. – Faheem Mitha Nov 21 '21 at 14:12
  • @TorbjørnT. The option you suggested works. Feel free to write an answer using that, if you wish. – Faheem Mitha Nov 21 '21 at 19:26

1 Answers1

2

As mentioned in comments, one option might be to use a TikZ \matrix, with the help of \usetikzlibrary{matrix}. Like a tabular, a row is ended with a \\, but note in particular that the last row must also end with \\

However, that might be overcomplicating things, \\[<distance>] works in a normal node where align=... is set as well, and the result is the same as far as I can see, given that the row sep and the \baselineskip match. So you can probably take your existing code and add [<distance>] as you see fit.

The baselineskip is the distance between the baselines of text lines. In \fontsize{10}{12}, the 10 indicates the font size and 12 the baselineskip in points. Note in the code below I set the row sep to 12pt, which is the same as is used in \fontsize{10}{12}. If you, in my example below, increase the row sep value to e.g. 15pt, and use \fontsize{10}{15} in the node settings, the lines will still line up.

The example below shows both methods, and adds some horizontal lines to indicate that apart from the padding (whitespace between node/matrix border and text), the result is the same. Left, matrix, right a normal node with align=left.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [draw,matrix of nodes,nodes={anchor=base west, font=\bfseries\strut}, row sep={12pt,between origins}] {Dr John Dolittle\\Oxenthorpe Road\\[20pt]Puddleby-on-the-Marsh\\[2cm]Slopshire, England\\};

% the positioning here is to vertically align the bottom rows of the node and matrix \path (m-4-1.base east) ++(1.5cm,0) node (a) [draw,anchor=base west, font=\fontsize{10}{12}\bfseries, align=left] {Dr John Dolittle\Oxenthorpe Road\[20pt]Puddleby-on-the-Marsh\[2cm]Slopshire, England};

% horizontal lines to indicate that the spacing is the same in both cases \foreach \Row in {1,2,3,4} \draw [red] (m-\Row-1.base west) -- ++(9cm,0); \end{tikzpicture} \end{document}

Torbjørn T.
  • 206,688
  • Thank you for the answer. In this phrase "apart from some the padding", the word "the" seems redundant. Is that correct? Also, I was wondering, when using the \\[<distance>] method, whether there was an easy way to compute the distance between lines. – Faheem Mitha Nov 22 '21 at 19:41
  • @FaheemMitha Yes, there was one word too many there, sorry about that. I don't understand what you mean by computing the distance between the lines, you specify the distance in the square brackets. What do you want to compute? – Torbjørn T. Nov 22 '21 at 19:48
  • Yes, that's the distance I was asking about. Is the amount specified in the square brackets the exact distance between lines, and if so, is it in cm? I read that for TikZ, values without units are measured in cm. – Faheem Mitha Nov 22 '21 at 19:51
  • @FaheemMitha The distance in the brackets is from baseline to baseline, i.e. the distance between the red lines in the image. As for the unit, you specify the unit as well. As you can see from the code, I used 20pt one place, 2cm another. – Torbjørn T. Nov 22 '21 at 19:53
  • Yes, I see it in the code. I was just wondering what the default units were. – Faheem Mitha Nov 22 '21 at 19:56
  • @FaheemMitha Judging by trial and error (which you could have done as well ...) the default is points. I've never thought about specifying a value like that without a unit before. The 1cm thing is about coordinates used in diagrams, because the default unit vectors are 1cm long. Baselineskips are another thing. – Torbjørn T. Nov 22 '21 at 20:00
  • Well, it's not obvious to me what the units are, even with trial and error. – Faheem Mitha Nov 23 '21 at 16:46
  • @FaheemMitha Basically, all I did for that was to change 20pt to 20 in my example and notice that the output didn't change .. However, I have to correct myself. The length in [] specify the additional line spacing. So with a default baselineskip of 12pt, \\[10pt] gives 22pt distance from one line to the next. Sorry about that. – Torbjørn T. Nov 23 '21 at 16:59
  • Ah, yes it looked to me like there was extra space. Thank you for the confirmation. And about switching to pts and noticing the output didn't change - that was clever. – Faheem Mitha Nov 23 '21 at 17:02