如何在出现单词的行首插入一个符号?

如何在出现单词的行首插入一个符号?

这是为了指示 LaTeX 文件的更改。比如说,我想在出现“单词 1”、“单词 2”... 或“单词 100”时在行首添加一条垂直线。可以手动完成此操作以确定“单词 1”出现的位置,但如果有很多“单词”,并且需要再次重复此过程,则可能会很繁琐。那么,有什么智能方法可以做到这一点呢?

I use this as an example to add vertical line 
| when word 1 appears and another vertical line
| word 2 appears.

答案1

我使用了我的软件包的改编版titlecaps,通常用于在给定一串单词的情况下将每个单词的首字母大写(用户指定的例外情况)。因此,我不会对单词进行限制,而是保持单词不变。但是,我使用搜索用户指定的例外情况的代码在左边距中放置规则并更改突出显示的单词的颜色(这两个功能独立运行,可以禁用任何一个而不会影响另一个)。

我使用该tabto包来方便边际线符号的表示。

要搜索的单词由宏指定\WordsToNote{word1 word2 word3},这是一个空格分隔的列表。后续调用是累积的,因此\WordsToNote{word1 word2}\WordsToNote{word3}功能上等同于前一次调用。可以使用重置单词列表\Resetlcwords

段落上的宏调用很简单\NoteWords{<text>}

titlecaps包一样,它可以处理有限的宏调用子集,包括文本大小更改和样式更改。

已编辑,可一次处理多个段落。已修复,因此\par在宏结束时不会自动发出新的。

\documentclass{article}
\usepackage{titlecaps}
\makeatletter
\renewcommand\titlecap[2][P]{%
  \digest@sizes%
  \if T\converttilde\def~{ }\fi%
  \redefine@tertius%
  \get@argsC{#2}%
  \seek@lcwords{#1}%
  \if P#1%
    \redefine@primus%
    \get@argsC{#2}%
    \protected@edef\primus@argi{\argi}%
  \else%
  \fi%
  \setcounter{word@count}{0}%
  \redefine@secundus%
  \def\@thestring{}%
  \get@argsC{#2}%
  \if P#1\protected@edef\argi{\primus@argi}\fi%
  \whiledo{\value{word@count} < \narg}{%
    \addtocounter{word@count}{1}%
    \if F\csname found@word\roman{word@count}\endcsname%
      \notitle@word{\csname arg\roman{word@count}\endcsname}%
      \expandafter\protected@edef\csname%
           arg\roman{word@count}\endcsname{\@thestring}%
    \else
      \notitle@word{\csname arg\roman{word@count}\endcsname}%
      \expandafter\protected@edef\csname%
           arg\roman{word@count}\endcsname{%
            \protect\MPAR\color{red}\@thestring\color{black}{}}%
    \fi%
  }%
  \def\@thestring{}%
  \setcounter{word@count}{0}%
  \whiledo{\value{word@count} < \narg}{%
    \addtocounter{word@count}{1}%
    \ifthenelse{\value{word@count} = 1}%
   {}{\add@space}%
    \protected@edef\@thestring{\@thestring%
      \csname arg\roman{word@count}\endcsname}%
  }%
  \let~\SaveHardspace%
  \@thestring%
  \restore@sizes%
\un@define}
\makeatother
\usepackage{tabto,xcolor}
\def\margrule{\protect\rule[-\dp\strutbox]{1pt}{\baselineskip}}
\def\MPAR{\protect\tabto*{-.2cm}%
  \margrule\protect\tabto*{\TabPrevPos}}
\let\WordsToNote\Addlcwords
\newcommand\NoteWords[1]{\NoteWordsHelp#1\par\relax}
\long\def\NoteWordsHelp#1\par#2\relax{%
  \titlecap{#1}%
  \ifx\relax#2\else\par\NoteWordsHelp#2\relax\fi%
}
\textwidth4in\relax\sloppy
\begin{document}
\WordsToNote{word1 word2 word3}
\NoteWords{
This is a test of finding  word1 and others like word2 and to see if
a marking can be placed in the \textit{margin when they} are found.
I also include word3 in the list.  \"Unfortunately, this only does
a single paragraph at a time.  \tiny Multiple ocurrences in a 
single row only result in a single\normalsize mark, with this word1 implementation.

For my second paragraph,
this is a test of finding  word1 and others like word2 and to see if
a marking can be placed in the \textit{margin when they} are found.
I also include word3 in the list.  \"Unfortunately, this only does
a single paragraph at a time.  \tiny Multiple ocurrences in a 
single row only result in a single\normalsize mark, with this word1 implementation.
}
Look Mom, no new paragraph.
\end{document}

在此处输入图片描述

请注意:我特意在 MWE 代码中放置了一行,\textwidth4in\relax\sloppy以减少边距并消除过满的框。在其他场地使用此代码之前,请将其删除。

相关内容