2015-12-29 37 views
1

您可以使用LaTeX读取文件并搜索关键词吗?你会如何做到这一点?乳胶:在文件中搜索关键字

我目前有读取文件和计算行数的代码。我在考虑在每行阅读过程中,我可以解析特定单词或标签的行。这可以做到吗?

\documentclass{book} 

\usepackage{ifthen} 

\newcounter{FileLines} 
\newboolean{RestFile} 
\newcommand{\FileLine}{} 
\newread\File 

\newcommand{\CountLinesInFile}[2] 
{ 
    \setboolean{RestFile}{true} 
    \setcounter{FileLines}{0} 

    \openin\File=#1 
    \whiledo{\boolean{RestFile}} 
    { 
    \ReadNextLine{\File} 
    \ifthenelse{\boolean{RestFile}}{ 
    \stepcounter{FileLines} 
    }{} 
    } 
    \closein\File 
} 

\newcommand{\ReadNextLine}[1]{ 
    \ifthenelse{\boolean{RestFile}}{ 
    \read#1 to \FileLine 
    \ifeof#1\setboolean{RestFile}{false} 
    \else % if last line already is read, EOF appears here 
    \fi 
    }{} 
} 

\begin{document} 

\CountLinesInFile{textfile.txt} 

Lines in file: \arabic{FileLines} 

\end{document} 
+0

只是一个想法,为什么不只是使用'\ index'来标记关键字? –

回答

0

它可能可以完成,但我不会推荐它。 TeX有许多限制和特性,使得它对于通用编程来说不是最理想的。

使用通用编程语言一样PythonRubyPerlLua将使任务很多容易。

我在这里特别包含了Lua,因为它被选作嵌入式脚本语言,它是下一代pdfTeX的luaTeX。有了luaTex,可以在您的TeX文档中嵌入 Lua代码。它还将TeX引擎的内部暴露给Lua,这为包装作家打开了一个全新的可能性。