将格式化命令作为参数传递

将格式化命令作为参数传递

\cleverul{text}我有一个由@Jinwen 提出的自定义下划线命令这里

我希望能够在\@makechapterhead宏的重新​​定义中传递命令,作为格式化值,其功能是漂亮地强调输出{\@chapapp{} \thechapter}(也可以用于章节名称,即##1)。

我想到解决方案是创建一个涉及\small-style 语法的变体。

问题是,有没有一种简单的方法可以将我的下划线命令转换\cleverul{text}{\coolclevul text}命令?

\cleverul或者,您是否找到了一种使用当前语法传递命令的方法?

平均能量损失

\documentclass{report}
\usepackage{etoolbox}
\usepackage{contour}
\usepackage{fp}
\usepackage[normalem]{ulem}

% Cool underlining. \cleverul

\contourlength{0.8pt}

\makeatletter

\newcommand{\cleverul}[1]
{%
    \FPmul\cleverul@temp{\f@size}{0.15}%
    \setlength{\ULdepth}{\cleverul@temp pt}%
    \FPdiv\cleverul@temp{\f@size}{15}% 
    \renewcommand{\ULthickness}{\cleverul@temp pt}%
    \uline{\phantom{#1}}%
    \llap{\contour{white}{#1}}%
}%


% Formatting Chapter-word and Chapter title

\NewDocumentCommand{\chapterformat}{m}{%
    \ifdef{\chapter}{%
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{50\p@}% 
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#1 \@chapapp{} \thechapter}% => Want to be able to pass my \cleverul command in formatting {\@chapapp{} \thechapter}.
                \par
                \vskip 20\p@
                \fi
                {\Huge\bfseries ##1} % Title formatting.
                \par\nobreak
                \vskip 180\p@}} 
    }{}
}

\makeatother

\begin{document}

    
    \chapterformat{\fontsize{60}{90}\selectfont\bfseries}  % => Want to be able to pass my \cleverul command.
    
    \chapter{Cool Underlining}
    
        \Huge\bfseries\cleverul{I love \LaTeX.}
    
    
\end{document}

在此处输入图片描述


问题解决

我最终使用了两个不同的键,chapterformat一个titleformat用于标准格式命令,chapterstyle另一个titlestyle用于涉及其参数分隔符的命令。

使用@Skillmon 的expkv-cs包如下这里,创建我自己key=value风格的自定义命令是小菜一碟。此变体还包括垂直间距。

\documentclass{report} % The report style has Chapters.

\usepackage{fontspec} % Allows me to switch to a "bold small caps"-compliant font.

\usepackage{etoolbox} % Provides \ifdef LaTeX wrapper.

\usepackage{contour} % Required for the \cleverul command.
\usepackage{fp} % Required for the \cleverul command.
\usepackage[normalem]{ulem} % Required for the \cleverul command.

\usepackage{expkv-cs}

\setmainfont{Times New Roman}

\newcommand{\giant}{\fontsize{72}{108}\selectfont}

% Cool underlining. \cleverul



\makeatletter

\newcommand{\cleverul}[1]
{%
    \contourlength{0.04ex}
    \FPmul\cleverul@temp{\f@size}{0.12}% The higher, the farther from the text. (font size multiplied by x)
    \setlength{\ULdepth}{\cleverul@temp pt}%
    \FPdiv\cleverul@temp{\f@size}{16}% The higher, the thinner. (font size divided by x)
    \renewcommand{\ULthickness}{\cleverul@temp pt}%
    \uline{\phantom{#1}}%
    \llap{\contour{white}{#1}}%
}%



\NewDocumentCommand{\chapterformat}{mmmmmmm}{
    \ifdef{\chapter}{
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{#5\p@}% Vertical Space before "ChapterX" (50)
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#3#1{\@chapapp{}~\thechapter}}
                \par
                \vskip #6\p@ % Vertical Space after "Chapter X" (20)
                \fi
                {#4#2{##1}}
                \par\nobreak
                \vskip #7\p@}} % Vertical Space after Title (180)
    }{}
}

\makeatother

\ekvcSplitAndForward\coolchap\chapterformat % Defining \coolchap as a split version of \chapterformat, using explkv package.
{
    chapstyle = \relax,
    titlestyle = \relax,
    chapformat  = \Huge\bfseries,
    titleformat = \Large,
    befchap    = 50,
    afchap     = 20,
    aftitle    = 180
}

\begin{document}
    

\coolchap{chapstyle=\cleverul,titlestyle=\textsc,chapformat=\giant\bfseries,titleformat=\Huge\bfseries,afchap=40}  
    
    \chapter{Cool Underlining}
    
    \Huge\bfseries\cleverul{I love \LaTeX.}
    
    
\end{document}

在此处输入图片描述

答案1

简单版本:只需将命令作为参数传递即可。其\cleverul本身的参数将是定义中紧随其后的任何内容#1。要对此进行分隔,您应该在定义中添加一对括号\chapterformat

\ifnum \c@secnumdepth >\m@ne
{#1{\@chapapp{} \thechapter}}% => Want to be able to pass my \cleverul command in formatting {\@chapapp{} \thechapter}.
\par
\vskip 20\p@
\fi
% ...
\chapterformat{\fontsize{60}{90}\selectfont\bfseries\cleverul}

更明确/结构化的版本:传递命令名称并使用\csname和构造宏和参数\endcsname。这些命令从字符串构造一个宏(一个控制序列),如果宏接受参数,则后面的方括号或花括号中的任何内容都将用作此宏的参数,否则在花括号的情况下,它将用作普通括号组。

通过它你可以做如下事情:

\csname cleverul\endcsname{text to underline}

其解释为:

\cleverul{text to underline}

还可以添加“常规”格式命令,以及允许仅有的常规格式化命令,您可以定义\chapterformat命令以接受两个参数,一个用于常规命令,一个用于 csname 构造。如果您不想使用 csname 部分,则可以传递relax,这将扩展为 ,\relax{some text}结果为{some text}除了\relax结束参数解析之外没有任何效果命令(参见\relax 起什么作用?)。

梅威瑟:

\documentclass{report}
\usepackage{etoolbox}
\usepackage{contour}
\usepackage{fp}
\usepackage[normalem]{ulem}

% Cool underlining. \cleverul

\contourlength{0.8pt}

\makeatletter

\newcommand{\cleverul}[1]
{%
    \FPmul\cleverul@temp{\f@size}{0.15}%
    \setlength{\ULdepth}{\cleverul@temp pt}%
    \FPdiv\cleverul@temp{\f@size}{15}% 
    \renewcommand{\ULthickness}{\cleverul@temp pt}%
    \uline{\phantom{#1}}%
    \llap{\contour{white}{#1}}%
}%


% Formatting Chapter-word and Chapter title

\NewDocumentCommand{\chapterformat}{mm}{%
    \ifdef{\chapter}{%
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{50\p@}% 
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#1\csname#2\endcsname{\@chapapp{} \thechapter}}% => Want to be able to pass my \cleverul command in formatting {\@chapapp{} \thechapter}.
                \par
                \vskip 20\p@
                \fi
                {\Huge\bfseries ##1} % Title formatting.
                \par\nobreak
                \vskip 180\p@}} 
    }{}
}

\makeatother

\begin{document}
    \chapterformat{\fontsize{60}{90}\selectfont\bfseries}{cleverul}  % => Want to be able to pass my \cleverul command.
    \chapter{Cool Underlining}
    \Huge\bfseries\cleverul{I love \LaTeX.}
    
    \chapterformat{\fontsize{60}{90}\selectfont\bfseries}{relax}  % => Want to be able to pass my \cleverul command.
    \chapter{Just large and bold}   
\end{document}

结果:

在此处输入图片描述

相关内容