有条件地打印带有彩色文本的驼峰式文本连字

有条件地打印带有彩色文本的驼峰式文本连字

在编写软件文档时,需要一条命令来自动将大写字母的驼峰式单词连字符化,例如 camel-Case。此外,这些驼峰式单词会排版为特定颜色。解决方案是此处提供通过命令\zzz{camelCase}

但是,有些 camelCase 变量仅供内部使用。为此,有一个标志定义为

\newboolean{\internal}
\setboolean{internal}{true}

在配置文件中。例如,如果整个段落仅供内部使用,则可以编写

\ifthenelse{\boolean{internal}}{%
    Here is a paragraph which will only appear when the \textbackslash internal flag is set to TRUE.%
}{}%

但这不是所有内部 camelCase 变量的实际实现。编写起来很麻烦,甚至会降低源代码的可读性。定义一个包含功能\zzz但也对内部标志做出反应的命令会更好。更准确地说,新命令的期望行为\zzzconditional{camelCase}将是:

  • 内部标志 TRUE:输入参数 camelCase 在大写字母处带连字符,并根据定义进行格式化\zzz{camelCase}
  • 内部标志 FALSE:输入参数 camelCase 从最终文档中完全消失。如果删除前面的空格,使缺失的 camelCase 不会在文档中留下空白,则可以获得加分。

因此我尝试了以下方法:

\newcommand{\zzzconditional}[1]{%
    \ifthenelse{\boolean{internal}}{%
        \zzz{#1}%
    }{}%
}

现在,我可以通过将它们写为 来打开或关闭文档中的内部变量\zzzconditional{camelCase}。但这会破坏命令的连字功能\zzz并导致行过满 - 即我回到了之前的起点\zzz

有什么方法可以“扩展”我的命令\zzzconditional以便\zzz在内部起作用?我希望“扩展”是乳胶俚语中的正确术语。如果解决方案还删除了我文本中的前一个空格以及 camelCase 变量,则可以获得加分。

如果解决方案实施起来太过艰难,请告诉我,因为我不想强加于人。我有一个 B 计划:使用 Python 扫描我的 latex 源代码以查找\zzz{}相关源代码并删除。但是,非 Pythonistas 也必须使用此文档,因此原生 latex 解决方案是首选。

我已经修改了平均能量损失来自先前的解决方案提供所有提到的命令的示例:

\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage{ifthen}
\showhyphens{createUnspecifiedNodeErrorMarker}
\def\zzcolor{\color{red}}

% --------------------------------------------
% Definition of \zzz
% --------------------------------------------
\makeatletter
\def\zzz{\leavevmode\begingroup
    \def\zzelt##1{%
        \catcode`##1\active\uccode`\~`##1\uppercase{%
            \def~{\egroup\egroup\-\hbox\bgroup\bgroup\zzcolor\string##1}}}%
    \zz@Alph{}%
    \@zzz}

\def\zz@Alph#1{%
    \zzelt A\zzelt B\zzelt C\zzelt D\zzelt E\zzelt F\zzelt G\zzelt H\zzelt I\zzelt J\zzelt
    K\zzelt L\zzelt M\zzelt N\zzelt O\zzelt P\zzelt Q\zzelt R\zzelt S\zzelt T\zzelt U\zzelt V\zzelt W\zzelt X\zzelt
    Y\zzelt Z}

\def\@zzz#1{\textbf{\hbox\bgroup\bgroup\zzcolor#1\egroup\egroup}\endgroup}
\makeatother

% --------------------------------------------
% Flag to set internal version of document
% --------------------------------------------
\newboolean{internal}
\setboolean{internal}{true}

% --------------------------------------------
% Definition of \zzzconditional which should work like \zzz, but only print to document if interal flag is TRUE
% --------------------------------------------
\newcommand{\zzzconditional}[1]{%
    \ifthenelse{\boolean{internal}}{%
        \zzz{#1}%
    }{}%
}

\begin{document}
    \section{Test}

    Here is an example of how \textbackslash zzz\{\} hyphenates camelCase words. This may also create overfull lines, but I can live with the result because the overflow is minimal and variables in the real document have more capitals than the one in this MWE:

    And another example the show must go on, but we have too less text (\zzz{createUnspecifiedNodeWarningMarker} and
    \zzz{createUnspecifiedNodeErrorMarker}, sdjklashjksa \zzz{createUnspecifiedLinkWarningMarker} and
    \zzz{createUnspecifiedLinkErrorMarker}).

    \ifthenelse{\boolean{internal}}{%
        Here is a paragraph which will only appear when the \textbackslash internal flag is set to TRUE. This is very helpful if I only want it to appear in the internal version of this example document. The next paragraph is visible in source code only because I change internal flag's value to FALSE.%
    }{}%

    \setboolean{internal}{false}
    \ifthenelse{\boolean{internal}}{%
        Now the internal flag has been set to FALSE which makes this paragraph disappear. This is very helpful if I only want it to appear in the internal version of this example document.%
    }{}%

    \setboolean{internal}{true}

    Here is an example of an overfull line because wrapping \textbackslash zzz\{\} inside \textbackslash zzzconditional\{\} does not work properly, as illustrated by this very long variable \zzzconditional{createUnspecifiedNodeWarningMarker}.
\end{document}

在此处输入图片描述

答案1

已编辑基于对 OP在 为假\zzzconditional时想要如何表现的澄清internal。本 MWE 的第二段和倒数第三段分别显示了 和 时internaltrue结果false

Jay_At_Play (OP) 编辑:包含 Steven 对\zzzconditional段落开头的错误修正(详情请参阅下面的评论)。辅助函数\removetheargument现在还调用\leavevmode。此 MWE 的最后一段显示了段落开头internal为 FALSE时的结果。\zzzconditional

在这里我重新定义

\newcommand{\zzzconditional}{%
    \ifthenelse{\boolean{internal}}{\zzz}{\removetheargument}%
}
\newcommand\removetheargument[1]{\leavevmode\unskip}

这样,\zzzconditional无需参数就可以调用它,并且根据 的布尔状态,要么重定向到\zzz,要么重定向到删除参数(和 s)的某个东西。\unskipinternal

\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage{ifthen}
\showhyphens{createUnspecifiedNodeErrorMarker}
\def\zzcolor{\color{red}}

% --------------------------------------------
% Definition of \zzz
% --------------------------------------------
\makeatletter
\def\zzz{\leavevmode\begingroup
    \def\zzelt##1{%
        \catcode`##1\active\uccode`\~`##1\uppercase{%
            \def~{\egroup\egroup\-\hbox\bgroup\bgroup\zzcolor\string##1}}}%
    \zz@Alph{}%
    \@zzz}

\def\zz@Alph#1{%
    \zzelt A\zzelt B\zzelt C\zzelt D\zzelt E\zzelt F\zzelt G\zzelt H\zzelt I\zzelt J\zzelt
    K\zzelt L\zzelt M\zzelt N\zzelt O\zzelt P\zzelt Q\zzelt R\zzelt S\zzelt T\zzelt U\zzelt V\zzelt W\zzelt X\zzelt
    Y\zzelt Z}

\def\@zzz#1{\textbf{\hbox\bgroup\bgroup\zzcolor#1\egroup\egroup}\endgroup}
\makeatother

% --------------------------------------------
% Flag to set internal version of document
% --------------------------------------------
\newboolean{internal}
\setboolean{internal}{true}

% --------------------------------------------
% Definition of \zzzconditional which should work like \zzz, but only print to document if interal flag is TRUE
% --------------------------------------------
\newcommand{\zzzconditional}{%
    \ifthenelse{\boolean{internal}}{\zzz}{\removetheargument}%
}
\newcommand\removetheargument[1]{\leavevmode\unskip}

\begin{document}
    \section{Test}

    Here is an example of how \textbackslash zzz\{\} hyphenates camelCase words. This may also create overfull lines, but I can live with the result because the overflow is minimal and variables in the real document have more capitals than the one in this MWE:

    And another example the show must go on, but we have too less text (\zzz{createUnspecifiedNodeWarningMarker} and
    \zzz{createUnspecifiedNodeErrorMarker}, sdjklashjksa \zzz{createUnspecifiedLinkWarningMarker} and
    \zzz{createUnspecifiedLinkErrorMarker}).

    \ifthenelse{\boolean{internal}}{%
        Here is a paragraph which will only appear when the \textbackslash internal flag is set to TRUE. This is very helpful if I only want it to appear in the internal version of this example document. The next paragraph is visible in source code only because I change internal flag's value to FALSE.%
    }{}%

    \setboolean{internal}{false}
    \ifthenelse{\boolean{internal}}{%
        Now the internal flag has been set to FALSE which makes this paragraph disappear. This is very helpful if I only want it to appear in the internal version of this example document.%
    }{}%

    \setboolean{internal}{true}

    Here is an example of an overfull line because wrapping \textbackslash zzz\{\} inside \textbackslash zzzconditional\{\} does not work properly, as illustrated by this very long variable \zzzconditional{createUnspecifiedNodeWarningMarker}.

    \setboolean{internal}{false}

    Here is an example of an overfull line because wrapping \textbackslash zzz\{\} inside \textbackslash zzzconditional\{\} does not work properly, as illustrated by this very long variable \zzzconditional{createUnspecifiedNodeWarningMarker}.
\end{document}

在此处输入图片描述

答案2

我不确定我是否完全理解您的要求,但是这是我的想法。

\documentclass{article}
\usepackage{xcolor}
\usepackage{microtype}
\usepackage{xparse,environ}


\ExplSyntaxOn
\NewDocumentCommand{\keyw}{O{.}m}
 {
  \bool_if:NTF \l_jayatplay_internal_bool
   { \unskip }
   { \textcolor{#1}{ \bfseries \jayatplay_keyw:n { #2 } } }
 }

\NewEnviron{internal}
 {
  \bool_if:NF \l_jayatplay_internal_bool { \BODY }
 }

\NewDocumentCommand{\setinternal}{O{true}}
 {
  \use:c { bool_set_#1:N } \l_jayatplay_internal_bool
 }

\bool_new:N \l_jayatplay_internal_bool

\cs_new_protected:Nn \jayatplay_keyw:n
 {
  \tl_map_inline:nn { #1 }
   {
    % if the current char is uppercase, add a discretionary hyphen
    \str_if_eq:eeT { ##1 } { \str_uppercase:n { ##1 } } 
     { \- }
    ##1
   }
 }
\ExplSyntaxOff

\begin{document}

\section{Test}

And another example the show must go on, but we have too less 
text (\keyw{createUnspecifiedNodeWarningMarker} sdjkle
\keyw{createUnspecifiedNodeErrorMarker}, sdjklashjksa 
\keyw{createUnspecifiedLinkWarningMarker} and
\keyw[red]{createUnspecifiedLinkErrorMarker}).

\section{Another test}

Here is an example of how \verb|\keyw{}| hyphenates camelCase words. 
This may also create overfull lines, but I can live with the result 
because the overflow is minimal and variables in the real document 
have more capitals than the one in this MWE:

And another example the show must go on, but we have too less text 
(\keyw{createUnspecifiedNodeWarningMarker} and \keyw{createUnspecifiedNodeErrorMarker}, 
sdjklashjksa \keyw{createUnspecifiedLinkWarningMarker} and \keyw{createUnspecifiedLinkErrorMarker}).

% the boolean starts out false, so this will appear
\begin{internal}
Here is a paragraph which will only appear when the \texttt{internal} flag is set to TRUE. 
This is very helpful if I only want it to appear in the internal version of this example 
document. The next paragraph is visible in source code only because I change internal 
flag's value to FALSE.
\end{internal}

% set the boolean to true
\setinternal

% this will not appear
\begin{internal}
Now the internal flag has been set to FALSE which makes this paragraph disappear.
This is very helpful if I only want it to appear in the internal version of this
example document.
\end{internal}

\setinternal[false]

Here is an example of an overfull line because wrapping \verb|keyw{}| inside 
\verb|\keyw{}| does not work properly, as illustrated by this very 
long variable \keyw{createUnspecifiedNodeWarningMarker}.


\setinternal

Here is an example of an overfull line because wrapping \verb|keyw{}| inside 
\verb|\keyw{}| does not work properly, as illustrated by this very 
long variable \keyw{createUnspecifiedNodeWarningMarker}.


\end{document}

在此处输入图片描述

相关内容