1

I have found a program that can convert PDF to LaTeX, and so I have .tex files of various math textbooks. I want to make a list of theorems from the book, and so I have been manually copying and pasting from the .tex file. Is there any way I can automate this process? For example, I could copy every line/paragraph that starts with the word theorem. Specifically, I am using LyX.

7
  • grep "^theorem" filename.tex > extracted.tex would find all lines beginning with 'theorem'. If you want to find 'TheOReM' as well, try grep -i "^theorem" instead.
    – cfr
    Commented Jan 1, 2014 at 1:41
  • @cfr He is asking for paragraphs as well, which should be multiline in the LaTeX source, so grep "^theorem" is not likely to produce the desired result.
    – Masroor
    Commented Jan 1, 2014 at 1:51
  • Depends on your source, I guess. My paragraphs would be seen as single lines in the source (usually). If you have your editor hard wrap or manually introduce line breaks then it would not work. sed or gawk would work better in that case. My point really is that trying to do this in your editor is probably a mistake.
    – cfr
    Commented Jan 1, 2014 at 2:57
  • 4
    By the way, what are you using to convert pdf to latex?
    – cfr
    Commented Jan 1, 2014 at 3:12
  • 2
    The \extract package (mirror.math.ku.edu/tex-archive/macros/latex/contrib/extract/…) can write the theorem environments in a tex file to another file. Commented Jan 1, 2014 at 17:54

2 Answers 2

1

This may not play nice with LyX (I haven't tested), but here is a solution to strip specific environments from a source file into a destination file. Define the function:

(require 'cl) ; place your cursor after this paren and pres C-x C-e

(defun **strip-stuff (file &optional environments)
  "Takes the current file and strips every environment from
`ENVIRONMENTS` into `FILE`."
  (interactive "FFile name: \nxList of Environments (\"one\" \"two\" \"etc\"): ")
  (if (every 'stringp environments)
      (progn
        (beginning-of-buffer)
        (let ((search-regex (concat "\\\\begin{"
                                    (mapconcat 'identity
                                               environments
                                               "\\|")
                                    "}")))
          (while (search-forward-regexp search-regex nil t)
            (LaTeX-mark-environment)
            (copy-region-as-kill (point) (mark))
            (save-excursion
              (find-file file)
              (end-of-buffer)
              (yank)
              (newline 2)
              (save-buffer)
              (previous-buffer))
            (exchange-point-and-mark)))
        (message "Strip complete.  Check %s for the output." file))
    (message (concat "The environment variable you provided"
                     " was not a list of strings.")))) ; here too

and just apply it (M-x **strip-stuff) within your file.

I've posted a video example of this working on YouTube. It demonstrates how to apply the function to multiple files automatically using a simple keyboard macro.

0

This is not a TeX related question. If you want to cut and paste just few paragraphs you will want to familiarize yourself with a serious text editor. I use nvi but typically you would want to check Vim and Emacs before considering other editors. For mass automation you will want to use at the very list to learn sed (streaming editor) and shell script or some kind a scripting language (Perl, Python comes to mind). Learning regular expressions is assumed.

Note that lack of native scripting language will render Windows OS useless for any serious text processing so you will have to use some kind UNIX or UNIX-like OS.

Disclaimer: I have heard for PowerShell for Windows but never really done anything serious with that OS.

9
  • There is Emacs for windows. I just don't know how applicable this is to the question; PDF is not particularly text-based. Commented Jan 1, 2014 at 3:13
  • @Sean Check his question again. He wants to cut and paste .tex source files. There is no Emacs for Windows. Check out you installation and you will see bunch of Cygwin stuff installed so that you can use Emacs on Windows. The same goes for Vin for Windows and so on. Python probably feels the most natural of all above tools on Windows but I started using computer long before Windows existed so I could not care less. Commented Jan 1, 2014 at 3:24
  • I don't have my installation near me (and won't until I return to campus) but it is not a minimal Cygwin or mingw install (I believe). My point was Emacs can be used on windows; you don't have to have a unix OS. If Emacs doesn't do the truck on its own, Cygwin is available. I can vouch that Powershell is effectively useless. Commented Jan 1, 2014 at 4:02
  • I did miss some content in the question. Again I don't have my computer and can't post a proper answer, but I'll leave a comment up top. Commented Jan 1, 2014 at 4:03
  • It is not really a TeX related question because the only good solutions are likely to involve generic text-processing tools. That is, as these comments show, platform specific. perl is available for Windows. TeX Live uses it.
    – cfr
    Commented Jan 1, 2014 at 4:12

You must log in to answer this question.

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