很难让 pgfkeys 按照我的需要设置字体大小

很难让 pgfkeys 按照我的需要设置字体大小

很抱歉,这篇文章不够短。

我正在研究旧代码并进行重构。在此过程中,我遇到了各种错误在我编写内容的方式中。在这个特定示例中,字体大小未正确初始化,我可以找出原因。

usefontsize/.initial我的问题是,除非我明确调用该密钥,否则我似乎无法使其生效fontsize

%-@-(1)-
\begin{filecontents}{samplecode}
% first line of material I don't want to see
  \documentclass{article}
  \begin{document}
  document content here
  \end{document}
\end{filecontents}
%-@-(2)-
\begin{filecontents}{ncode}
\makeatletter
\def\ae@box@fbox{}
\def\ae@mboxheight{}
\def\ae@value@is@none{}
\def\code@cat{%
  \renewcommand{\FancyVerbFormatLine}[1]{##1}%
  \ae@ncode@set{fontsize/relsize={-0.75},
                examcodecolor,
                input/method={\VerbatimInput
                       [gobble=\ae@ncode@get{gobble},
                        firstline=\ae@ncode@get{firstline},
                        lastline=\ae@ncode@get{lastline},
                        firstnumber=\ae@ncode@get{firstnumber},
                        numbers=\ae@ncode@get{numbers},
                        numbersep=\ae@ncode@get{numbersep},
                        fontfamily=courier,
                        fontsize=\relsize{-0.75},
                       ]}}}
\pgfkeys
  {%
    /ae/ncode/.is family,
    /ae/ncode,
    %%:
    input/method/.initial,
    %%:
    examcode/.code       = {\code@cat},
    %%:
    gobble/.initial        = 2,
    width/.initial         = 3in,
    firstline/.initial     = 1,
    lastline/.initial      = 5000,
    firstnumber/.initial   = 1,
    rulecolor/.initial     = black,
    fillcolor/.initial     = red!20,
    numbers/.initial       = none,
    numbersep/.initial     = 1em,
    indent/.initial        = 0pt,
    %%:
    usefontsize/.initial     = \normalsize,
    fontsize/.style          = { usefontsize=\csname #1\endcsname},
    fontsize/relsize/.style  = { usefontsize=\relsize{#1}},
    %%:
    examcodecolor/.style   = { fillcolor=#1 },
    examcodecolor/.default = red!30,
    %%:
    mboxheight/.initial = \ae@mboxheight,
    height/.code = {\expandafter\ifx\csname ae@value@is@#1\endcsname\ae@value@is@none\else
                    \edef\ae@mboxheight{[#1]}\fi
                     },
    %%:
    /ae/ncode/.cd,
    fbox/.style = { select/box/style/#1/.get = \ae@box@fbox, }, 
    select/box/style/.cd,
      none/.initial,
      fbox/.initial      = \fbox,
      fcolorbox/.initial = \fcolorbox{\ae@ncode@get{rulecolor}}{\ae@ncode@get{fillcolor}},                   
  }
\def\ae@ncode@get#1{\pgfkeysvalueof{/ae/ncode/#1}}
\def\ae@ncode@set{\pgfqkeys{/ae/ncode}}
\newcommand{\nncode}[2][]{%
  \pgfqkeys{/ae/ncode}{usefontsize,#1}% <--- doesn't seem to matter whether `usefontsize` is here or not
  \edef\ae@begin@minipage{\noexpand\minipage[t]\ae@ncode@get{mboxheight}{\dimexpr\ae@ncode@get{width}-\ae@ncode@get{indent}}}%
  \ae@box@fbox{%
    \hspace*{\ae@ncode@get{indent}}%'
    \ae@begin@minipage
      \ae@ncode@get{usefontsize}% <--- doesn't seem to be taking effect in left version
      \ae@ncode@get{input/method}{#2}%'
    \endminipage}%' 
}
\makeatother
\end{filecontents}
%-@-(3)-
\documentclass{article}
\usepackage{pgfkeys}
\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{xcolor}
\usepackage[margin=0.5in]{geometry}
\input{ncode}
\setlength{\parindent}{0pt}
\begin{document}

Shouldn't the left and the right boxes look identical?

{\nncode[examcode,
        width={\dimexpr2.25in-2\fboxrule-2\fboxsep},
        fbox=fcolorbox,
        height=none,
        firstline=6,
        gobble=2,
        indent=0.5em,
        fontsize=normalsize,
       ]{samplecode}}%
%%'
{\nncode[examcode,
        width={\dimexpr2.25in-2\fboxrule-2\fboxsep},
        fbox=fcolorbox,
        height=none,
        firstline=6,
        gobble=2,
        indent=0.5em,
%        fontsize=normalsize,
       ]{samplecode}}%

\end{document}

在此处输入图片描述

我知道这段代码的某些部分我可以写得不一样。例如,我本来可以只写一个关键字fontsize来接受,而不是一个空词,而是字体的实际命令名称。但请记住,我正在重构旧代码。我刚开始学习使用时,这段代码的旧版本键值,不允许我传递命令名称。我现在有数百行代码依赖于旧语法。因此,我需要一个解决方案来识别关键字需要fontsize接受一个裸词(而不是宏名称)作为其值。同样,我处理关键字的方式也很奇怪,height因为在我的旧代码中它并不总是被指定为维度。

编辑

我应该指出,还有其他按键,我也可以做类似的事情欺骗 与其他已初始化的键一起使用。例如,examcodecolor重置fillcolor,但无论是否examcodecolor实际调用,似乎都可以正常工作。稍微偏离常规的是height,但如果您重新定义\ae@mboxheight为:

\def\ae@mboxheight{[2in]}

无需调用height任何正确的参数值即可使用。

有没有我没有考虑到的特殊之处\normalsize?在我定义的时候它是否以某种方式被调用了usefontsize/.initial

相关内容