9

I'm looking for a command, which would take a text and print it with the first letter formatted differently:

\first{Hello, world!}

Should be turned into:

\textbf{\large H}ello, world!

Is there a package, which would do this?

yegor256
  • 12,021

2 Answers2

12

If the only aversion to lettrine is breaking your spell checker, then just bundle it inside another macro, \first.

PDFLATEX

Since the OP asked about pdflatex in another comment, I introduce \pervyy, which bundles the 2-byte letter sequence that makes up Cyrillic characters, for use in \lettrine.

\documentclass{article}
\usepackage{lettrine}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\newcommand\first[1]{\firstaux#1\relax}
\def\firstaux#1#2\relax{\lettrine{#1}{#2}}
\newcommand\pervyy[1]{\pervyyaux#1\relax}
\def\pervyyaux#1#2#3\relax{\lettrine{#1#2}{#3}}
\begin{document}
\first{This} is a test of using\\
lettrine without breaking \\
the spell-check features.

\pervyy{Привет,} мир! \end{document}

enter image description here

LUALATEX OR XELATEX

If one wanted to use an engine other than pdflatex, then omit the T2A encoding, load an appropriate font supporting the Cyrillic alphabet, and just use \first, regardless of which alphabet is employed:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{EB Garamond} % choose a suitable text font
\usepackage{lettrine}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\newcommand\first[1]{\firstaux#1\relax}
\def\firstaux#1#2\relax{\lettrine{#1}{#2}}
\begin{document}
\first{This} is a test of using\\
lettrine without breaking \\
the spell-check features.

\first{Привет,} мир! \end{document}

enter image description here

7

If you're willing and able to compile your document with LuaLaTeX, the following solution may be of interest to you.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{fontspec}
\setmainfont{EB Garamond} % choose a suitable text font

\usepackage{luacode} % for "\luaexec" macro \luaexec{ function first ( s ) local t t = "\textbf{\large " .. unicode.utf8.sub(s,1,1) .. "}" tex.sprint ( t .. unicode.utf8.sub(s,2) ) end } \newcommand\first[1]{\luaexec{ first ( "#1" ) }}

\begin{document} \first{Hello, world!}

\first{Привет, мир!} \end{document}

Mico
  • 506,678
  • @DavidCarlisle - Thanks. I've updated the code to handle the case of \first{Привет, мир!}. The main change, I think, consisted of loading the fontspec package and loading a text font that contains Cyrillic characters. :-) – Mico Sep 12 '21 at 13:06
  • thanks :-)...... – David Carlisle Sep 12 '21 at 14:56
  • Is it at all possible to do the same (or similar) in pdflatex? – yegor256 Sep 12 '21 at 16:49
  • Of course there's the challenge of correctly handling, say A + ´ or other Unicode glyphs that are built out of combining characters, for example ‍‍ is actually five unicode characters, or क्‍ which is three (ka + virāma + ZWJ). (the semantics of ZWJ are their own little mess. In Emjoi contexts it's infix, but for Indic scripts, it's (mostly) postfix. – Don Hosek Sep 12 '21 at 18:18
  • @yegor256 - LaTeX3 provides a pretty good string library, but I haven't mastered LaTeX3 (yet). But I'm pretty good with Lua and LuaTeX... – Mico Sep 12 '21 at 18:54
  • @DonHosek - I may have interpreted the OP's criterion, "the first letter of a word", to narrowly. :-) – Mico Sep 12 '21 at 18:55