自动以小写字母显示每个引文的第一个单词

自动以小写字母显示每个引文的第一个单词

考虑一下代码

\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}

相关内容