测试当前颜色是否为黑色(带换行符)

测试当前颜色是否为黑色(带换行符)

这个问题源于这个帖子,原始海报定义了宏\TypsetInBlueOnlyIfCurrentColorIsBlack;仅当当前颜色为黑色时,它才会将文本的颜色更改为蓝色。

我使用了原始帖子中提供的代码,然后在使用新宏时添加了一个换行符;下面是代码。

代码:

\documentclass{article}
\listfiles %% first thing Anna added
\usepackage{xcolor}
\usepackage{xstring}

%% At this point the default color is black so save that.
\edef\BlackColor{\csname\string\color@.\endcsname}

\makeatletter
\newcommand{\@CurrentColor}{}% Make sure we are not using an existing macro
\newcommand*{\TypsetInBlueOnlyIfCurrentColorIsBlack}[1]{%
    \edef\@CurrentColor{\csname\string\color@.\endcsname}% Get current color
    \IfStrEq{\@CurrentColor}{\BlackColor}{%
        {\textcolor{blue}{#1}}% current color IS black
    }{%
        {#1}%  current color is NOT black
    }%
}
\makeatother

\begin{document}


    Following is blue:
    \TypsetInBlueOnlyIfCurrentColorIsBlack{blue}.

\color{red}
    Following is red:
    \TypsetInBlueOnlyIfCurrentColorIsBlack{red}.

\color{blue}
    Following is blue:
    \TypsetInBlueOnlyIfCurrentColorIsBlack{blue
    
    
    with a linebreak}. %%%% Second and last thing Anna added
\end{document}

问题:

为什么我会收到以下错误?

Runaway argument?
{blue 
! Paragraph ended before \TypsetInBlueOnlyIfCurrentColorIsBlack was complete.
<to be read again> 
                   \par 
l.34 
     
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

我已经在 overleaf 上完成了此操作,并预先清除了所有辅助文件。

日志档案:

为了完整起见,这里是日志。

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=pdflatex 2020.9.10)  17 MAY 2022 22:07
entering extended mode
 \write18 enabled.
 %&-line parsing enabled.
**name.tex
(/compile/name.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-07-17> (/usr/local/texlive/2020/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2019/12/20 v1.4l Standard LaTeX file (size option)
)
\c@part=\count168
\c@section=\count169
\c@subsection=\count170
\c@subsubsection=\count171
\c@paragraph=\count172
\c@subparagraph=\count173
\c@figure=\count174
\c@table=\count175
\abovecaptionskip=\skip47
\belowcaptionskip=\skip48
\bibindent=\dimen134
) (/usr/local/texlive/2020/texmf-dist/tex/latex/xcolor/xcolor.sty
Package: xcolor 2016/05/11 v2.12 LaTeX color extensions (UK)
(/usr/local/texlive/2020/texmf-dist/tex/latex/graphics-cfg/color.cfg
File: color.cfg 2016/01/02 v1.6 sample color configuration
)
Package xcolor Info: Driver file: pdftex.def on input line 225.
(/usr/local/texlive/2020/texmf-dist/tex/latex/graphics-def/pdftex.def
File: pdftex.def 2018/01/08 v1.0l Graphics/color driver for pdftex
)
Package xcolor Info: Model `cmy' substituted by `cmy0' on input line 1348.
Package xcolor Info: Model `hsb' substituted by `rgb' on input line 1352.
Package xcolor Info: Model `RGB' extended on input line 1364.
Package xcolor Info: Model `HTML' substituted by `rgb' on input line 1366.
Package xcolor Info: Model `Hsb' substituted by `hsb' on input line 1367.
Package xcolor Info: Model `tHsb' substituted by `hsb' on input line 1368.
Package xcolor Info: Model `HSB' substituted by `hsb' on input line 1369.
Package xcolor Info: Model `Gray' substituted by `gray' on input line 1370.
Package xcolor Info: Model `wave' substituted by `hsb' on input line 1371.
) (/usr/local/texlive/2020/texmf-dist/tex/generic/xstring/xstring.sty (/usr/local/texlive/2020/texmf-dist/tex/generic/xstring/xstring.tex
\integerpart=\count176
\decimalpart=\count177
)
Package: xstring 2019/02/06 v1.83 String manipulations (CT)
) (/usr/local/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-pdfmode.def
File: l3backend-pdfmode.def 2020-06-29 L3 backend support: PDF mode
\l__kernel_color_stack_int=\count178
\l__pdf_internal_box=\box45
) (/compile/output.aux)
\openout1 = `output.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 21.
LaTeX Font Info:    ... okay on input line 21.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 21.
LaTeX Font Info:    ... okay on input line 21.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 21.
LaTeX Font Info:    ... okay on input line 21.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 21.
LaTeX Font Info:    ... okay on input line 21.
LaTeX Font Info:    Checking defaults for TS1/cmr/m/n on input line 21.
LaTeX Font Info:    ... okay on input line 21.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 21.
LaTeX Font Info:    ... okay on input line 21.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 21.
LaTeX Font Info:    ... okay on input line 21.
(/usr/local/texlive/2020/texmf-dist/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
\scratchcounter=\count179
\scratchdimen=\dimen135
\scratchbox=\box46
\nofMPsegments=\count180
\nofMParguments=\count181
\everyMPshowfont=\toks15
\MPscratchCnt=\count182
\MPscratchDim=\dimen136
\MPnumerator=\count183
\makeMPintoPDFobject=\count184
\everyMPtoPDFconversion=\toks16
)
Runaway argument?
{blue 
! Paragraph ended before \TypsetInBlueOnlyIfCurrentColorIsBlack was complete.
<to be read again> 
                   \par 
l.34 
     
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

! Too many }'s.
l.36         with a linebreak}
                              . %%%% Second and last thing Anna added
You've closed more groups than you opened.
Such booboos are generally harmless, so keep going.

[1

{/usr/local/texlive/2020/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] (/compile/output.aux)

 *File List*
 article.cls    2019/12/20 v1.4l Standard LaTeX document class
  size10.clo    2019/12/20 v1.4l Standard LaTeX file (size option)
  xcolor.sty    2016/05/11 v2.12 LaTeX color extensions (UK)
   color.cfg    2016/01/02 v1.6 sample color configuration
  pdftex.def    2018/01/08 v1.0l Graphics/color driver for pdftex
 xstring.sty    2019/02/06 v1.83 String manipulations (CT)
l3backend-pdfmode.def    2020-06-29 L3 backend support: PDF mode
supp-pdf.mkii
 ***********

 ) 
Here is how much of TeX's memory you used:
 1571 strings out of 480906
 21619 string characters out of 5908280
 262885 words of memory out of 5000000
 17227 multiletter control sequences out of 15000+600000
 535088 words of font info for 29 fonts, out of 8000000 for 9000
 1141 hyphenation exceptions out of 8191
 25i,5n,37p,226b,107s stack positions out of 5000i,500n,10000p,200000b,80000s
</usr/local/texlive/2020/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on /compile/output.pdf (1 page, 14135 bytes).
PDF statistics:
 12 PDF objects out of 1000 (max. 8388607)
 7 compressed objects within 1 object stream
 0 named destinations out of 1000 (max. 500000)
 1 words of extra memory for PDF output out of 10000 (max. 10000000)

答案1

当你这样做

\newcommand*{\TypsetInBlueOnlyIfCurrentColorIsBlack}[1]{%
  ...
}

你告诉 LaTeX\TypsetInBlueOnlyIfCurrentColorIsBlack 不能包含空行(或\par)。

使用

\newcommand{\TypsetInBlueOnlyIfCurrentColorIsBlack}[1]{%
  ...
}

相关内容