我有一个逗号分隔的列表,我用 来处理\foreach
。这已经工作了一段时间。决定做一个小调整,即允许可选的行距,在这种情况下,我想删除 * 并将该特定行标记为红色。这似乎有效,除了内容 ( ) 包含其他宏*
的情况。\the\MyListB
问题:
我需要做哪些更改\DisplayListFormatted
才能处理\DisplayListFormatted{\the\MyListB}
并让以星号开头的行变为红色?
下面的 MWE 的输出(关键行被注释掉)是:
代码:
\documentclass{article}
\usepackage{xstring}
\usepackage{amsmath}
\usepackage{pgffor}
\usepackage{xcolor}
\usepackage{xparse}
\newtoks{\MyListA}
\newtoks{\MyListB}
\MyListA={
2016-05-25: {Some plain comment, but has commas so is brace grouped}.,
*2016-05-26: Some text with macros and "math" $sin^2 x + cos^2 x \ne 0$.,
2016-05-27: Some other important comment.,
}
\MyListB={
2016-05-25: {Some plain comment, but has commas so is brace grouped}.,
*2016-05-26: Some text with \emph{macros} and math $\sin^2 x + \cos^2 x \ne 0$.,
2016-05-27: Some other \emph{important} comment.,
}
\newcommand{\DisplayListUnformatted}[1]{%
\edef\MyExpandedList{#1}%
\foreach \x in \MyExpandedList {%
\par\hspace*{2.0ex}\x%
}
}%
\newcommand{\DisplayListFormatted}[1]{%
\edef\MyExpandedList{#1}%
\foreach \x in \MyExpandedList {%
\par\hspace*{2.0ex}%
%\noexpandarg% <--Can process \MyListB if uncomment this, but asterix detection fails!
\IfBeginWith{\x}{*}{%
\StrGobbleLeft{\x}{1}[\xNoAsterix]%
\color{red}\xNoAsterix%
}{%
\x%
}%
}%
}%
\begin{document}
Displaying the list works:
\par\verb|\the\MyListA|
\DisplayListUnformatted{\the\MyListA}
\par\verb|\the\MyListB|
\DisplayListUnformatted{\the\MyListB}
But want to extract the asterix:
\par\verb|\the\MyListA|
\DisplayListFormatted{\the\MyListA}
\par\verb|\the\MyListB|
%\DisplayListFormatted{\the\MyListB}% <--- How do I get this to work!
\end{document}
答案1
需要扩展\x
一次。
\documentclass{article}
\usepackage{xstring}
\usepackage{amsmath}
\usepackage{pgffor}
\usepackage{xcolor}
\usepackage{xparse}
\newtoks{\MyListA}
\newtoks{\MyListB}
\MyListA={
2016-05-25: {Some plain comment, but has commas so is brace grouped}.,
*2016-05-26: Some text with macros and "math" $sin^2 x + cos^2 x \ne 0$.,
2016-05-27: Some other important comment.,
}
\MyListB={
2016-05-25: {Some plain comment, but has commas so is brace grouped}.,
*2016-05-26: Some text with \emph{macros} and math $\sin^2 x + \cos^2 x \ne 0$.,
2016-05-27: Some other \emph{important} comment.,
}
\newcommand{\DisplayListUnformatted}[1]{%
\edef\MyExpandedList{#1}%
\foreach \x in \MyExpandedList {%
\par\hspace*{2.0ex}\x
}
}
\newcommand{\DisplayListFormatted}[1]{%
\edef\MyExpandedList{#1}%
\foreach \x in \MyExpandedList {%
\par\hspace*{2.0ex}%
\noexpandarg
\expandafter\IfBeginWith\expandafter{\x}{*}{%
\expandafter\StrGobbleLeft\expandafter{\x}{1}[\xNoAsterix]%
\color{red}\xNoAsterix%
}{%
\x
}%
}%
}
\begin{document}
Displaying the list works:
\par\verb|\the\MyListA|
\DisplayListUnformatted{\the\MyListA}
\par\verb|\the\MyListB|
\DisplayListUnformatted{\the\MyListB}
But want to extract the asterix:
\par\verb|\the\MyListA|
\DisplayListFormatted{\the\MyListA}
\par\verb|\the\MyListB|
\DisplayListFormatted{\the\MyListB}
\end{document}
您还可以享受xparse
实现。
\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\ExplSyntaxOn
\NewDocumentCommand{\newlist}{mm}
{
\clist_new:c { g_peter_list_#1_clist }
\clist_gset:cn { g_peter_list_#1_clist } { #2 }
}
\NewDocumentCommand{\DisplayListFormatted}{m}
{
\clist_map_inline:cn { g_peter_list_#1_clist }
{
\par
\hspace*{1em}
\tl_if_eq:nfTF { * } { \tl_head:n { ##1 } }
{ \textcolor{red} { \tl_tail:n { ##1 } } }
{ ##1 }
}
}
\cs_generate_variant:Nn \tl_if_eq:nnTF { nf }
\ExplSyntaxOff
\newlist{A}{
2016-05-25: {Some plain comment, but has commas so is brace grouped}.,
*2016-05-26: Some text with macros and "math" $sin^2 x + cos^2 x \ne 0$.,
2016-05-27: Some other important comment.,
}
\newlist{B}{
2016-05-25: {Some plain comment, but has commas so is brace grouped}.,
*2016-05-26: Some text with \emph{macros} and math $\sin^2 x + \cos^2 x \ne 0$.,
2016-05-27: Some other \emph{important} comment.,
}
\begin{document}
List A
\DisplayListFormatted{A}
List B
\DisplayListFormatted{B}
\end{document}
答案2
由于您的格式化激活是使用 完成的*
,因此您可以使用宏样式方法来检测它\@ifstar
,类似于对或 的\chapter
条件:\@chapter
\@schapter
\documentclass{article}
\usepackage{pgffor,xcolor}
\newtoks{\MyListA}
\newtoks{\MyListB}
\MyListA={
2016-05-25: {Some plain comment, but has commas so is brace grouped}.,
*2016-05-26: Some text with macros and ``math'' $sin^2 x + cos^2 x \neq 0$.,
2016-05-27: Some other important comment.
}
\MyListB={
2016-05-25: {Some plain comment, but has commas so is brace grouped}.,
*2016-05-26: Some text with \emph{macros} and math $\sin^2 x + \cos^2 x \neq 0$.,
2016-05-27: Some other \emph{important} comment.
}
\makeatletter
\def\processitem{\@ifstar\processitem@i\processitem@ii}
\def\processitem@i#1\@nil{\color{red}#1}
\def\processitem@ii#1\@nil{#1}
\newcommand{\DisplayListUnformatted}[1]{%
\edef\MyExpandedList{#1}%
\foreach \x in \MyExpandedList {%
\par\hspace*{2.0ex}\x%
}
}%
\newcommand{\DisplayListFormatted}[1]{%
\edef\MyExpandedList{#1}%
\foreach \x in \MyExpandedList {%
\par\hspace*{2.0ex}%
\expandafter\processitem\x\@nil
}%
}%
\makeatother
\begin{document}
Displaying the list works:
\par\verb|\the\MyListA|
\DisplayListUnformatted{\the\MyListA}
\par\verb|\the\MyListB|
\DisplayListUnformatted{\the\MyListB}
But want to extract the asterisk:
\par\verb|\the\MyListA|
\DisplayListFormatted{\the\MyListA}
\par\verb|\the\MyListB|
\DisplayListFormatted{\the\MyListB}
\end{document}