5

I have a command \foo which takes two arguments and which I use about a hundred times throughout my document (but never within an argument of another instance of \foo). I now want to spellcheck every first argument in language A and every second argument in language B. There is a lot of other text in my document, which I do not wish to spellcheck.

The main problem here is parsing all first or second arguments of \foo. I could in theory write a script that extracts this, but I suppose somebody has already done this and taken care of all caveats and special cases. So, essentially I am looking for a tool that allows me to extract all arguments of a certain command throughout a document. Feeding the output into a spellchecker shouldn’t be a problem.

I am on Linux, so I should be able to get most things running. I prefer solutions that write the arguments to stdout or in a file.

The following questions and answers do not solve my problem satisfactorily:

3 Answers 3

2

I think that a Perl script (or whatever scripting language you like most) is more efficient. But it's possible to do it also in TeX.

Taking Werner's input as model, here's a way for writing out two files, containing the first and second arguments to \foo, respectively.

\documentclass{article}

\usepackage{xcolor}

\colorlet{langA}{red!30}
\colorlet{langB}{blue!30}

\newcommand{\foo}[2]{%
  % the original definition (supply yours)
  \textcolor{langA}{#1}--\textcolor{langB}{#2}%
  % the added part
  \write\langAout{\detokenize{#1}}\write\langBout{\detokenize{#2}}%
}
\newwrite\langAout
\newwrite\langBout
\immediate\openout\langAout=\jobname-a.out
\immediate\openout\langBout=\jobname-b.out

\begin{document}

Lorem ipsum dolor sit amet, \foo{consectetur}{adipiscing} elit. Phasellus elementum 
urna quis \foo{mi}{fermentum}, sit amet congue ex lobortis. Vivamus vulputate turpis 
neque, nec rutrum magna tempor ac. \foo{Sed}{pulvinar} tristique tempor. Sed venenatis 
tincidunt nunc, sit amet dictum eros. Proin sed consequat turpis. \foo{Nulla}{ut} mauris 
lacinia \foo{lacus}{sollicitudin} imperdiet at ut lorem. Sed in efficitur ante. Nulla 
scelerisque, \foo{massa}{pharetra} dictum vestibulum, lectus diam elementum ante, id 
ultricies lectus tellus sed turpis. Phasellus \foo{nec}{maximus} dolor. Cras vehicula 
tincidunt quam finibus fringilla. Maecenas eget leo nec quam \foo{venenatis}{sagittis}. 
\foo{Etiam}{eget} est id mauris mollis egestas nec eget sapien. Suspendisse aliquet massa 
at urna mattis fermentum. \foo{Quisque}{quis} sem turpis. Donec viverra ornare leo, sit 
amet consectetur lacus eleifend a. Fusce \foo{elementum}{sem} volutpat ante mattis luctus.

Proin \foo{volutpat}{justo} quis fringilla viverra. Integer erat lectus, facilisis at 
posuere a, maximus at libero. \foo{Phasellus}{malesuada} tincidunt turpis quis condimentum. 
Ut eget nibh id lorem vulputate sagittis. Pellentesque \foo{habitant}{morbi} tristique 
senectus et netus et malesuada \foo{fames}{ac} turpis egestas. Etiam et malesuada magna. 
Pellentesque dictum \foo{blandit}{est}, at condimentum nunc volutpat a. Donec venenatis 
orci sapien, eu placerat ligula \foo{pulvinar}{eget}.

\end{document}

If the main file is named test.tex, you'll get two files named test-a.out and test-b.out:

test-a.out

consectetur
mi
Sed
Nulla
lacus
massa
nec
venenatis
Etiam
Quisque
elementum
volutpat
Phasellus
habitant
fames
blandit
pulvinar

test-b.out

adipiscing
fermentum
pulvinar
ut
sollicitudin
pharetra
maximus
sagittis
eget
quis
sem
justo
malesuada
morbi
ac
est
eget
1
  • Accepting the answer as it extracts the contents into a file and thus makes further handling with a spell checker easier (in contrast to Werner’s answer).
    – Wrzlprmft
    Sep 3, 2015 at 16:54
3

The following is an adaptation of Extract all emphasized words from LaTeX document, now only for two arguments. The first arguments are stored in \firstfoo, while the second arguments are stored in \secondfoo.

enter image description here

\documentclass{article}
\newcommand{\foo}[2]{%
  % <your original definition>
}

\makeatletter
\newcommand{\firstfoo}{}
\newcommand{\secondfoo}{}
\renewcommand{\foo}[2]{%
  \textit{#1} \textbf{#2}% Just for display
  \g@addto@macro{\firstfoo}{#1\space}% Add first argument to \firstfoo
  \g@addto@macro{\secondfoo}{#2\space}% Add second argument to \secondfoo
}
\makeatother

\begin{document}

Lorem ipsum dolor sit amet, \foo{consectetur}{adipiscing} elit. Phasellus elementum 
urna quis \foo{mi}{fermentum}, sit amet congue ex lobortis. Vivamus vulputate turpis 
neque, nec rutrum magna tempor ac. \foo{Sed}{pulvinar} tristique tempor. Sed venenatis 
tincidunt nunc, sit amet dictum eros. Proin sed consequat turpis. \foo{Nulla}{ut} mauris 
lacinia \foo{lacus}{sollicitudin} imperdiet at ut lorem. Sed in efficitur ante. Nulla 
scelerisque, \foo{massa}{pharetra} dictum vestibulum, lectus diam elementum ante, id 
ultricies lectus tellus sed turpis. Phasellus \foo{nec}{maximus} dolor. Cras vehicula 
tincidunt quam finibus fringilla. Maecenas eget leo nec quam \foo{venenatis}{sagittis}. 
\foo{Etiam}{eget} est id mauris mollis egestas nec eget sapien. Suspendisse aliquet massa 
at urna mattis fermentum. \foo{Quisque}{quis} sem turpis. Donec viverra ornare leo, sit 
amet consectetur lacus eleifend a. Fusce \foo{elementum}{sem} volutpat ante mattis luctus.

Proin \foo{volutpat}{justo} quis fringilla viverra. Integer erat lectus, facilisis at 
posuere a, maximus at libero. \foo{Phasellus}{malesuada} tincidunt turpis quis condimentum. 
Ut eget nibh id lorem vulputate sagittis. Pellentesque \foo{habitant}{morbi} tristique 
senectus et netus et malesuada \foo{fames}{ac} turpis egestas. Etiam et malesuada magna. 
Pellentesque dictum \foo{blandit}{est}, at condimentum nunc volutpat a. Donec venenatis 
orci sapien, eu placerat ligula \foo{pulvinar}{eget}.

\verb|\firstfoo|: {\itshape \firstfoo}

\verb|\secondfoo|: {\bfseries \secondfoo}

\end{document}
0

As a general scheme:

\newif\iflanguageA
\def\foo#1#2{\iflanguageA your action on #1\else your action on #1 \fi}

With usage:

\languageAtrue

\foo12, 

\languageAfalse

\foo12.

You have conditional \iflanguageA (the name is to be chosen) and Boolean values \languageAtrue and \languageAfalse.

1
  • This describes how to create such a command, but not how to perform a spellcheck on all of its first or second arguments. I thus fail to see how this answers my question.
    – Wrzlprmft
    Sep 3, 2015 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .