![自动以小写字母显示每个引文的第一个单词](https://linux22.com/image/462899/%E8%87%AA%E5%8A%A8%E4%BB%A5%E5%B0%8F%E5%86%99%E5%AD%97%E6%AF%8D%E6%98%BE%E7%A4%BA%E6%AF%8F%E4%B8%AA%E5%BC%95%E6%96%87%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8D%95%E8%AF%8D.png)
考虑一下代码
\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
%\usepackage[sfdefault]{cabin}
\newcommand{\longemdash}{{\fontfamily{cmss}\selectfont---}}
\newcommand{\emdash}{\nobreak---\nobreak\hskip0pt}
\newcommand{\fancyquote}[2]{%
\par\noindent{#1}\\*\hspace*{\fill}\longemdash\fontfamily{cmss}\selectfont\textit{\textbf{#2}}%
}
\begin{document}
\thispagestyle{empty}
\fancyquote{{\scshape{Baseball}} is 90 percent mental. The other half is physical.}{Yogi Berra}
\vspace*{20pt}
\fancyquote{If people don't want to come to the ballpark, how are you going to stop them?}{Yogi Berra}
\vspace*{20pt}
\fancyquote{You can observe a lot by watching.}{Yogi Berra}
\end{document}
产生
{\scshape{Baseball}
问题:如何才能自动以小写形式显示每个引语的第一个单词,而不必每次都求助于某种方式?
谢谢。
答案1
用分隔参数隔离第一个单词。
\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{cabin}
\usepackage{showframe}
\newcommand{\longemdash}{\makebox[1em][s]{---\hss---}}
\makeatletter
\newcommand{\fancyquote}[2]{%
\fancyquote@#1\@nil{#2}%
}
\def\fancyquote@#1 #2\@nil#3{%
\par\noindent\textsc{#1} #2\\*\hspace*{\fill}\longemdash{\itshape\bfseries#3}%
}
\makeatother
\begin{document}
\thispagestyle{empty}
\fancyquote{Baseball is 90 percent mental. The other half is physical.}{Yogi Berra}
\vspace*{20pt}
\fancyquote{If people don't want to come to the ballpark, how are you going to stop them?}{Yogi Berra}
\vspace*{20pt}
\fancyquote{You can observe a lot by watching.}{Yogi Berra}
\end{document}
答案2
您的第一个\fancyquote
不会在之后重置字体,因此您后续的文本仍属于 cmss 系列。我认为这是无意的,所以我在解决方案中进行了更改。
我的解决方案在纯 TeX 中有效,它只是用空格分隔 TeX 宏中的参数。
\font\twelverm=cmr10 at 12pt
\font\twelvesc=cmcsc10 at 12pt % Roman small caps
\font\twelveecso=ecso1200 % cmss bold italic
\font\twelveecss=ecss1200 % cmss default
\twelverm % Also set math fonts to 12pt here
\def\fancyquoteA#1 {{\twelvesc#1} }
\def\fancyquote#1#2{\par\noindent\fancyquoteA#1 \par\hfill{\twelveecss---\twelveecso#2}\par}
\fancyquote{Baseball is 90 percent mental. The other half is physical.}{Yogi Berra}
\vskip20pt
\fancyquote{If people don't want to come to the ballpark, how are you going to stop them?}{Yogi Berra}
\vskip20pt
\fancyquote{You can observe a lot by watching.}{Yogi Berra}
\bye
这可以轻松转换为更多的 LaTeX 代码,您可以删除\font
定义并用 LaTeX 字体宏替换它们。
答案3
这是一个基于 LuaLaTeX 的解决方案。它利用 Lua 的内置字符串函数来识别字符串开头的“单词”(一个或多个字母字符的序列),并将该单词呈现为小写字母。(字符串开头的空格是允许,即从字符串中删除它。)如果感兴趣的字符串不是以上面定义的单词开头,则该字符串将不加修改地输出。
% !TEX TS-program = lualatex
\documentclass[12pt]{book}
%% Lua-side code
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
function first_word_sc ( s )
s = s:gsub ( "^%s*(%a+)" , "\\textsc{%1}" )
tex.sprint ( s )
end
\end{luacode}
%% LaTeX-side code: utility macro that invokes the Lua function
\newcommand\FirstWordSC[1]{%
\directlua { first_word_sc ( "#1" ) }}
\usepackage{fontspec}
%\setmainfont{Latin Modern Roman}
%\setsansfont{Latin Modern Sans}
\newfontfamily{\Cabin}{Cabin Regular}
\newcommand{\longemdash}{\textsf{\textemdash}}
\newcommand{\fancyquote}[2]{%
\par\noindent{\Cabin\FirstWordSC{#1}}\\*%
\hspace*{\fill}\longemdash\textsf{\textit{\textbf{#2}}}%
}
\begin{document}
\fancyquote{Baseball is 90 percent mental. The other half is physical.}{Yogi Berra}
\vspace*{20pt}
\fancyquote{If people don't want to come to the ballpark, how are you going to stop them?}{Yogi Berra}
\vspace*{20pt}
\fancyquote{ You can observe a lot by watching.}{Yogi Berra}
\end{document}