Fitting text to a shape

A given text is fitted to specified paths. This code automatically uses the shapepar package with TikZ.

The shapeparnode macro uses six parameters:

  1. (optional - default: empty) a style for the node (font size, color...)
  2. the horizontal margin (a distance)
  3. the vertical margin (a distance)
  4. the left boundary (a continuous vertical path)
  5. the right boundary (a continuous vertical path)
  6. the text (a single paragraph without par or empty line)

The shapeparnodeaccuracy can be locally redefined and gives the accuracy to compute the shape (default: 2 lines per em).

The code was written by Paul Gaborit and posted on TeX.SE. Slight modification for using english blind text.


text-shape

Edit and compile if you like:

% Fitting text to a shape
% Author: Paul Gaborit
\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage{shapepar}
\usepackage{microtype}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{10pt}%
\usetikzlibrary{calc,fit,intersections}
\def\shapeparnodeaccuracy{2}
\newcommand\shapeparnode[6][]{
% 6 parameters:
% style for node (default:empty),
% h margin, v margin, left path, right path, text (just one paragraph!)
% name left and right paths and compute there bounding boxes
\begin{scope}[local bounding box=leftbb]
\path[name path global=left, xshift=#2] #4;
\end{scope}
\node[inner ysep=-#3, inner xsep=0pt, fit=(leftbb)](leftbb){};
\begin{scope}[local bounding box=rightbb]
\path[name path global=right, xshift=-#2] #5;
\end{scope}
\node[inner ysep=-#3, inner xsep=0pt, fit=(rightbb)](rightbb){};
% global bounding box
\path let
\p1=(leftbb.north west), \p2=(leftbb.south west),
\p3=(rightbb.north east), \p4=(rightbb.south east)
in
\pgfextra{
\pgfmathsetmacro{\ymin}{(\y1 < \y3) ? \y1 : \y3}
\pgfmathsetmacro{\ymax}{(\y2 > \y4) ? \y2 : \y4}
\typeout{ymin \ymin}
\typeout{ymax \ymax}
} node[inner sep=0, fit={(\x1,\ymin pt)(\x3,\ymax pt)}](mybb){};
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Click to download: text-shape.textext-shape.pdf
Open in Overleaf: text-shape.tex