6

I made a template that

  1. splits text into first letter and the rest
  2. creates a path to an image on disk based on the first letter

I'm having issues with the next step:

  1. I'm trying to use \lettrine to display the text with an image as the initial letter (drop cap style).

Here's what I have so far:

\documentclass[12pt]{article}
\usepackage[paperwidth=5in, paperheight=3in, margin=0.2in]{geometry}
\usepackage{graphicx, lettrine, xcolor, background, dirtytalk}

% split text into the first letter and the rest
\newcommand{\splittext}[1]{%
  \def\firstletter{\expandafter\splitfirst#1\empty}%
  \def\resttext{\expandafter\splitrest#1\empty}%
}

\def\splitfirst#1#2\empty{#1}
\def\splitrest#1#2\empty{#2}


\begin{document}
\pagecolor{white}
\vspace*{\fill}

\splittext{text and even more text}
\firstletter
\par
\resttext

% dynamically construct the image path from \firstletter
\edef\imagepath{/path/to/alphabet/\MakeUppercase{\firstletter}.png}
\imagepath

\end{document}

the above works displaying:

t

ext and even more text

/path/to/alphabet/T.png

if I add (right before \end{document})

\expandafter\lettrine\expandafter[image=true, lines=3, findent=3pt, nindent=0pt]{\imagepath}{}\resttext

I get this error:

Missing \endcsname inse rted. let l.30 ...s=3, findent=3pt, nindent=0pt]{\imagepath}{} \resttext 1269 words of node memory still in use: 9 hlist, 2 rule, 4 disc, 3 local_par, 2 dir, 22 glue, 5 kern, 4 penalty, 73 glyph, 15 attribute, 49 glue_spec, 15 attribute_list, 1 temp, 2 if_stack, 1 wri te, 4 pdf_colorstack nodes avail lists: 2:9,3:2,5:1,7:1 /path/to/latextemplates/.initial.tex.swp:30: ==> Fatal error occurr ed, no output PDF file produced! Transcript written on .initial.tex.log.

While if I pass the actual path, everything works

\lettrine[image=true, lines=3, findent=3pt, nindent=0pt]{/path/to/alphabet/T.png}{}\resttext

How should I pass a dynamically constructed path? Is there a better package than lettrine for this?

Please explain your solution.

here's T.png

1
  • \expandafter\lettrine\expandafter[i expands i which isn't expandable so the \expandafter are doing nothing (it's not clear what effect you intended them to have?) Commented Aug 8 at 20:46

2 Answers 2

5

\str_uppercase:f, \str_head_ignore_spaces:n and \str_tail_ignore_spaces:n from LaTeX 2ε's expl3-programming-layer might do the trick:

\documentclass[12pt]{article}
%\usepackage[paperwidth=5in, paperheight=3in, margin=0.2in]{geometry}
%\usepackage{graphicx, lettrine, xcolor, background, dirtytalk}

\usepackage{graphicx, lettrine, xcolor}

\begin{document}

%-------------------------------------------------------------------

\ttfamily\selectfont \frenchspacing \parindent=0pt \parskip=.5\baselineskip

Attempting to define the imagepath via \string\MakeUppercase:

% split text into the first letter and the rest
\newcommand{\splittext}[1]{%
  \def\firstletter{\expandafter\splitfirst#1\empty}%
  \def\resttext{\expandafter\splitrest#1\empty}%
}%
\def\splitfirst#1#2\empty{#1}%
\def\splitrest#1#2\empty{#2}%
%
\splittext{text and even more text}%
%
\makeatletter
\protected@edef\imagepath{/path/to/alphabet/\MakeUppercase{\firstletter}.png}%
\makeatother
%
\string\firstletter: \meaning\firstletter

\string\resttext: \meaning\resttext

\string\imagepath: \meaning\imagepath
%-------------------------------------------------------------------
\vskip\parskip\hrule
%-------------------------------------------------------------------

Attempting to define the imagepath via expl3 routines:

\ExplSyntaxOn
\cs_new_eq:NN \expluppercasef \str_uppercase:f
\cs_new_eq:NN \explstrhead \str_head_ignore_spaces:n
\cs_new_eq:NN \explstrtail \str_tail_ignore_spaces:n
\ExplSyntaxOff
%
\makeatletter
\protected@edef\firstletter{\explstrhead{text and even more text}}%
\protected@edef\resttext{\explstrtail{text and even more text}}%
\protected@edef\imagepath{/path/to/alphabet/\expluppercasef{\explstrhead{text and even more text}}.png}%
\makeatother
%
\string\firstletter: \meaning\firstletter

\string\resttext: \meaning\resttext

\string\imagepath: \meaning\imagepath
%-------------------------------------------------------------------
\vskip\parskip\hrule
%-------------------------------------------------------------------

\lettrine[image=true, lines=3, findent=3pt, nindent=0pt]%
         {/path/to/alphabet/\expluppercasef{\explstrhead{text and even more text}}.png}{}%
         \explstrtail{text and even more text}

\end{document}

enter image description here


With this answer it was decided not to use expl3's \text_lowercase:n / \text_uppercase:n because filepaths/filenames seem to be programmatic data and interface3.pdf, section "34.2 Case changing" says:

For case changing programmatic data see the l3str module and discussion there of \str_lowercase:n, \str_uppercase:n and \str_foldcase:n.

4

The argument to \includegraphics needs to expand to the path and \MakeUppercase isn't expandable (for reasons of historical compatibility) but the underlying expl3 command is, so:

enter image description here

\documentclass[12pt]{article}
\usepackage[paperwidth=5in, paperheight=3in, margin=0.2in]{geometry}
\usepackage{graphicx, lettrine,lmodern}

% split text into the first letter and the rest
\newcommand\mlettrine[1]{\xmlettrine#1\relax}

\ExplSyntaxOn
\def\xmlettrine#1#2\relax{%
  \lettrine{\includegraphics[height=25pt]{path/\text_uppercase:n{#1}}}{#2}}
\ExplSyntaxOff



\begin{document}


\mlettrine{text and even more text}
abc abc abc abc abc abc abc abc abc abc 
abc abc abc abc abc abc abc abc abc abc 
abc abc abc abc abc abc abc abc abc abc 
abc abc abc abc abc abc abc abc abc abc 
abc abc abc abc abc abc abc abc abc abc 
abc abc abc abc abc abc abc abc abc abc 
\end{document}

assuming the T image is saved in path/T.png relative to the current directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .