0

I know that stackexchange is filled with helpful stuff about how to do things similar to what I want to do, but I'm afraid I'm not skilled enough to extrapolate from them and solve my problem.

I'm trying to write a script that searches for specific latex definitions, contained in one or more .sty files, and then return the entire definition. The definition is contained inside curly brackets, but there may be lots of curly brackets inside of the definition.

For example, following this thread I tried the command

sed -n '/\\def\\propref\>/,/}$/p' *.sty

but this returns

\def\propref#1{%
        \IfBeginWith*{#1}{prop:}

But I want it to return the entire definition, delimited by {}, i.e.,

\def\propref#1{%
        \IfBeginWith*{#1}{prop:}
            {Prop.~\ref{#1}}%
            {Prop.~\ref{prop:#1}}}

So, the hard part for me is to locate the closing delimiter that matches the opening one. A second issue, if the solution is to use sed, is that I'd like the command to return the file name as well as the pattern, just as grep does, when searching thru multiple files. Specifically, I'll like the first line of the returned output to look grep-like

my_oneLineDefs.sty:\def\propref#1{%

Here's a snippet of the .sty file containing the definition.

\def\propref#1{%
    \IfBeginWith*{#1}{prop:}
        {Prop.~\ref{#1}}%
        {Prop.~\ref{prop:#1}}}

\def\thmref#1{%
    \IfBeginWith*{#1}{thm:}
        {Thm.~\ref{#1}}%
        {Thm.~\ref{thm:#1}}}

\def\secref#1{%
    \IfBeginWith*{#1}{sec:}
        {\S\ref{#1}}%
        {\S\ref{sec:#1}}}
Leo Simon
  • 186
  • 1
  • 9
  • 1
    Possible duplicate of [How to select lines between two patterns?](https://stackoverflow.com/questions/38972736/how-to-select-lines-between-two-patterns) – Sundeep Dec 09 '17 at 05:02
  • Thanks @Sundeep, the solution in that thread doesn't work for me, because of a delimiter matching issue, I've expanded the question, illustrating the problem. – Leo Simon Dec 09 '17 at 17:07
  • you can check that second line has only `}` and spaces... try `sed -n '/\\def\\propref/,/^ *}$/p'` also note that `\>` is not available in all sed versions... you can also use `#` character present in line if that helps – Sundeep Dec 10 '17 at 01:58
  • Thanks @Sundeep, unfortunately your suggestion isn't restrictive enough in my context because not all definitions in the file are terminated by a line that has only } and spaces. I've modified the code snippet. I think the only thing that's going to work is if I can actually find that match of the outside delimiter – Leo Simon Dec 10 '17 at 16:12
  • 1
    Attempting to parse a fairly complex format like LaTeX source with simple regular expressions is going to be painful. Maybe look at https://tex.stackexchange.com/questions/4223/what-parsers-for-latex-mathematics-exist-outside-of-the-tex-engines – tripleee Dec 10 '17 at 18:45

0 Answers0