1

I am searching for the whole evening, but couldn't find a solution. (I also read Remove a specific latex command from the text AND closing bracket behind it)

I have a lot of LaTeX files from which I would love to extract the argument of a special command into second file.

Please imagine, a LaTeX file with lots of "short" lines, i.e. a line break all 80 chars. Thus it is more than likely, that the argument of the command in question, spans more than one line in the source file. Furthermore, there might be some extra LaTeX commands with additional curly braces, which should be extracted as well.

Lorem ipsum dolor sit amet, \ltxcmd{consetetur sadipscing} elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
erat, sed diam voluptua. At vero eos et accusam et justo \ltxcmd{duo
dolores \emph{et ea rebum}}. Stet clita kasd gubergren, \ltxcmd{sea takimata
\textbf{sanctus} \emph{est} Lorem} ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea
rebum.

This should return the three arguments as follows:

{consetetur sadipscing}
{duo dolores \emph{et ea rebum}}
{sea takimata \textbf{sanctus} \emph{est} Lorem}

I tried first

cat file1.tex | sed -n 's/.*\\ltxcmd\({[[:alnum:] ]*}\).*/\1/p'

but this ended searching at the line end, therefore I tried

tr \n ' ' < file1.tex | sed -n 's/.*\\ltxcmd\({[[:alnum:] ]*}\).*/\1/p'

which returned the first occurance, but leaves the rest untouched.

Next try was to a gat the end of the sed expression, in order to start the search anew -- not helpful.

Any hints? bash and sed would be welcome.

  • Bash isn’t much of a text editor – Jeff Schaller Apr 4 '18 at 21:36
  • Jeff, I know, but they are both programmable. I have to work on >50 files on this project, any automatic help would be appreciated. I wasn't even able, to get sed to search beyond the line break. When I used tr to remove the line end, I ended up with only the first result, skipping the remaining targets in that file. :-( – Jan Apr 4 '18 at 21:43
  • You need a recursive decent parser here, you can write it with or without a regex. Either way, wouldn't want to do this in bash. – Evan Carroll Apr 4 '18 at 22:45
  • Think this probably more belongs on TeX - LaTeX – Evan Carroll Apr 4 '18 at 22:53
  • @EvanCarroll this could be any kind of programming language, it just happens in my case to be LaTeX. I tried something like tr "\n" " " < infile | sed -n 's/.*\\ltxcmd\({[[:alnum:] ]*}\).*/\1/p', in my bash, but this prints only the first occurance and misses every other occurance. Of course, this was just a POC. If if succeeded, I would have added find . -name *.tex - exec ... {};` also ... – Jan Apr 5 '18 at 4:53
1

This won't answer your question, but it's too long for a comment.

It will be complex in Bash or any command line. They main difficulty, I think, is that you will have to count open { to decide which one is the close bracket } as they can be nested and in different lines. I would recommend a Python script as the best tool to solve this problem.

To play around with the regular expressions, you can use Notpad++ (it works with wine). Something like \\ltxcmd(\{.*\}) would work if it wasn't for the nested {} that I mentioned.

A starting point for regular expression search in Python https://stackoverflow.com/questions/454456/how-do-i-re-search-or-re-match-on-a-whole-file-without-reading-it-all-into-memor

  • thank you very much. I didn't intended to use something like VI or Emacs as tool, as I have a lot of files to act on. It was merely a hint, to have any kind of script. Your hint, although not fully solving the problem, is clearly something, I was looking fore. Please, accept, that I I won't click the solved button, yet. – Jan Apr 6 '18 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.