更改代码中注释中显示的内容?

更改代码中注释中显示的内容?

我认识到 LaTeX 不太可能做到这一点,所以这可能归结为“是否有任何第三方 LaTeX 编辑软件......”

作为一名大学教师,我为学生编写的练习集很长。为了轻松找到文档,我尝试在注释中包含适当的标签和解释。例如

% Question 5: Differentiate a product
\item <question>
<worked out solution>

% Question 6: Differentiate a quotient
\item <question>
<worked out solution>

问题是,当我有一个很长的列表(50 个或更多问题)时,我决定在列表中间添加一些问题。现在我在评论中的编号被关闭了。为了修复它们,我必须手动浏览整个文件并更改每个问题。

那么,有没有办法定义某种计数器或标签,让我可以提出新问题并重新编号评论?同样,这是我查看代码时看到的:

% Question 5: Differentiate a product
\item <question>
<worked out solution>

% Question 6: NEW QUESTION
\item <question>
<worked out solution>

% Question 7: Differentiate a quotient
\item <question>
<worked out solution>

并且代码中的注释已重新编号。

答案1

这是一个AWK解决方案,而不是LaTeX解决方案,所以也许应该将其移到另一个论坛。

如果您的系统上有AWKGAWK解释器,您可以使用以下脚本自动重新编号您的源文件。

BEGIN { questionNum = 1; }
{
    if ($1 == "%" && $2 == "Question")
    {
        out = $1" "$2" "questionNum":";
        for (i = 4; i <= NF; i++) { out = out" "$i; }
        print out;
        ++questionNum;
    }
    else { print $0; }
}

原始文件:

% Question 5: Differentiate a product
\item <question>
<worked out solution>

% Question x: NEW QUESTION
\item <question>
<worked out solution>

% Question 7: Differentiate a quotient
\item <question>
<worked out solution>

已处理文件:

% Question 1: Differentiate a product
\item <question>
<worked out solution>

% Question 2: NEW QUESTION
\item <question>
<worked out solution>

% Question 3: Differentiate a quotient
\item <question>
<worked out solution>

答案2

您可以使用以下方式将其作为“单行”操作perl;例如,您可以使用:

perl -p -e 's/(?<!\\)%(\h*Question\h*)\d+/$i++;"%$1$i"/e' htg.tex

例如htg.tex

% Question 5: Differentiate a product
\item <question>
<worked out solution>

% Question 6: NEW QUESTION
\item <question>
<worked out solution>

% Question 7: Differentiate a quotient
\item <question>
<worked out solution>

%Question 4: Differentiate a quotient
\item <question>
<worked out solution>

%Question5: Differentiate a quotient
\item <question>
<worked out solution>

this one has a percentage symbol, \%     Question 5: Differentiate a quotient
\item <question>
<worked out solution>

%Question6: Differentiate a quotient
\item <question>
<worked out solution>

当(小心!风险自负) 使用perl -p -i.bak -e 's/(?<!\\)%(\h*Question\h*)\d+/$i++;"%$1$i"/e' htg.tex

% Question 1: Differentiate a product
\item <question>
<worked out solution>

% Question 2: NEW QUESTION
\item <question>
<worked out solution>

% Question 3: Differentiate a quotient
\item <question>
<worked out solution>

%Question 4: Differentiate a quotient
\item <question>
<worked out solution>

%Question5: Differentiate a quotient
\item <question>
<worked out solution>

this one has a percentage symbol, \%     Question 5: Differentiate a quotient
\item <question>
<worked out solution>

%Question6: Differentiate a quotient
\item <question>
<worked out solution>

几点说明:

  • -p告诉perl逐行处理文件(htg.tex),并打印输出
  • -e是允许我们在引号内execute运行代码的标志perl
  • -i.bak只有当你确信这个东西有用时才可以使用;它会覆盖htg.tex并创建htg.tex.bak备份。
  • s/this/that/替换thisthat
  • (?<!\\)%(\h*Question\h*)\d+表示一个%,前面不能有\,然后后跟零个或多个水平空格,然后Question,然后是零个或多个空格,然后是一个或多个数字;()是“捕获”括号,其结果存储在特殊变量 中$1; -$i++;"%$1$i"增加一个计数器,$i,然后,最后,%与 的结果$1和 的新值一起输出$i
    • /e末尾的修饰符允许我们在替换(扩展模式)中执行这些操作。

相关内容