5

This is to indicate changes for a LaTeX file. Say, I wanted to add a vertical line to the beginning of a line whenever"word 1", "word 2", ...", or "word 100" appears. Manually this can be done in order to determine where "word 1" appears, but it can be tedious if there are many "word" and if the process needs to be repeated again. So what is the intelligent way to do this?

I use this as an example to add vertical line 
| when word 1 appears and another vertical line
| word 2 appears.
Werner
  • 603,163
noname
  • 51
  • 2
    A Minimum Working Example would be useful. For instance, is this to be applied over a whole document, or only a subset? Will the text that has to be searched contain macros (for example, \textit{}, etc.), math, etc.? Is using an editor (search and replace) in conjunction with LaTeX a suitable option, or does the process have to occur purely in the LaTeX end of it? Are the markings for all the words the same, or does each word have its own marginal marking? – Steven B. Segletes Apr 30 '15 at 16:40
  • As I said, for a Latex file. A minimum working example was displayed above. Let's forget about \textit{} and see the above simple example. – noname Apr 30 '15 at 16:58

1 Answers1

5

I use an adaptation of my titlecaps package, normally used for capitalizing the first letter of each word, given a string of words (with user-specified exceptions). Therefore, instead of capping words, I leave them unchanged. However, I use the code that searches for the user-specified exceptions to both place a rule in the left margin and to change the color of the highlighted word (those two features operate independently and either can be disabled without affecting the other).

I use the tabto package to facilitate the marginal line notation.

Words to be searched for are specified by the macro \WordsToNote{word1 word2 word3}, a space-separated list. Subsequent invocations are cumulative, so that \WordsToNote{word1 word2}\WordsToNote{word3} is functionally equivalent to the former invocation. The list of words may be reset with \Resetlcwords.

The macro invocation on the paragraph is simply \NoteWords{<text>}.

As with the titlecaps package, it can handle a limited subset of macro invocations including text size changes and style changes.

EDITED to handle multiple paragraphs at a time. FIXED so that a new \par is not automatically issued at the end of the macro.

\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}

enter image description here

Please note: the line \textwidth4in\relax\sloppy I purposefully placed in the MWE code to reduce the margins and eliminate overfull boxes. Please remove them before using this code in other venues.

  • It is quite impressive for the example. But I have more than one paragraph in a file. – noname Apr 30 '15 at 17:44
  • @noname Give me a few minutes. – Steven B. Segletes Apr 30 '15 at 17:45
  • @noname Please see revision. – Steven B. Segletes Apr 30 '15 at 18:00
  • This really works! Thanks but I found this also changed my latex file margins since I have a pre-specified file format. I was wondering what lines in your file I have to modify so that my file format keeps the same except those vertical lines. – noname Apr 30 '15 at 18:14
  • @noname Eliminate the line \textwidth4in\relax\sloppy. I purposefully reduced the margins just to force a few extra lines in my output. – Steven B. Segletes Apr 30 '15 at 18:18
  • Very nice. There is a potential problem if I have to make the vertical lines within the margin (right now the lines are outside the margin)- how do I indent the vertical lines so everything including the vertical lines are in the margin? – noname Apr 30 '15 at 19:19
  • @noname You could enclose the stuff between \begin{quotation}...\end{quotation}. But if you didn't like the extra vertical and right-margin spacing of that, one might try \noindent\hspace*{1cm}\begin{minipage}[t]{\dimexpr\textwidth-1cm}\WordsToNote{word1 word2 word3}\NoteWords{...}\end{minipage}\par\strut. The two occurences of "1cm" can be changed to suit. I should add that the -.2cm argument to \tabto can also be tuned, which will change the offset of the vertical bar to the left. – Steven B. Segletes Apr 30 '15 at 19:33
  • It works fine but in a real Latex file with citations for instance, \citep, then things can become complicated and do not t work nicely. – noname May 01 '15 at 17:42
  • @noname I said up front that the method can only handle a limited number of macros, relating to fontsize and font style. – Steven B. Segletes May 02 '15 at 00:57