获取文档标题的首字母

获取文档标题的首字母

我试图获取文档标题的首字母,但没有得到。我使用的代码如下:

\usepakage{xtring}
\StrLeft{\thetitle}{1}[\firstletter]

编译结果:

未定义控制序列。

<argument> \thetitle l.439 \StrLeft{\thetitle}{1} [\firstletter] The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., `\hobx'), type `I' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined. \c@numresumo=\count339 \c@teorema=\count340 \c@proposicao=\count341 \c@lema=\count342 \c@corolario=\count343 \c@exemplo=\count344 \c@observacao=\count345 \c@definicao=\count346 \c@quadro=\count347 \c@loqdepth=\count348 \cftbeforequadroskip=\skip303 \cftquadroindent=\skip304 \cftquadronumwidth=\skip305 \cftbeforealgorithmtskip=\skip306 \cftalgorithmtindent=\skip307 \cftalgorithmtnumwidth=\skip308 \cftbeforelstlistingskip=\skip309 \cftlstlistingindent=\skip310 \cftlstlistingnumwidth=\skip311 \c@countcodigo=\count349 \c@count=\count350 \output@idxfile=\write10 Writing index file output.idx ) (/usr/local/texlive/2014/texmf-dist/tex/latex/rotating/rotating.sty Package: rotating 2009/03/28 v2.16a rotated objects in LaTeX \c@r@tfl@t=\count351 \rotFPtop=\skip312 \rotFPbot=\skip313 \rot@float@box=\box69 \rot@mess@toks=\toks65 ) (/usr/local/texlive/2014/texmf-dist/tex/generic/xypic/xy.sty (/usr/local/texlive/2014/texmf-dist/tex/generic/xypic/xy.tex Bootstrap'ing: catcodes, docmode, (/usr/local/texlive/2014/texmf-dist/tex/generic/xypic/xyrecat.tex) (/usr/local/texlive/2014/texmf-dist/tex/generic/xypic/xyidioms.tex) 

答案1

问题之所以发生,是因为\StrLeft我们看到\thetitle——而不是内容\thetitle\StrLeft真的不知道在这种情况下该怎么办。

您需要控制何时\thetitle扩展:

\expandafter\StrLeft\expandafter{\thetitle}{1}[\firstletter]

或者,使用expl3

\documentclass{memoir}
\usepackage{xstring}

\usepackage{expl3}

\title{bleh}
\begin{document}

\ExplSyntaxOn
\exp_args:NV \StrLeft \thetitle {1} [\firstletter]
\ExplSyntaxOff

\firstletter
\end{document}

expl3乍一看可能比较难懂,但它是一致的,并且很多更清楚最终会发生什么。 \exp_args:NV将采取前两个命​​令(称为代币在 TeXspeak 中)并重新调用它们,只是这次将第二个命令扩展为其值。texdoc interface3有关此命令和其他好东西的更多信息,请参阅。

相关内容