unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Convert some Latex expressions in Emacs.
@ 2022-05-16  3:25 Hongyi Zhao
  2022-05-16  4:49 ` Emanuel Berg
  2022-05-17  0:02 ` Michael Heerdegen
  0 siblings, 2 replies; 12+ messages in thread
From: Hongyi Zhao @ 2022-05-16  3:25 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1584 bytes --]

Currently, I've some LaTeX code OCRed into Emacs from a book, as shown
below and in the attached file:

```
\begin{array}{l}G_{192}^{1} \\ P^{12}=E ; Q^{4}=E ; R^{4}=E ; S^{8}=E
; Q^{2}=R^{2} ; S^{2}=P^{6} Q^{2} R ; Q P=P^{7} Q^{2} R ; Q
P^{3}=P^{3} Q \\ R P=P^{10} Q R ; R Q=P^{6} Q^{3} R ; S P=P^{2} R S ;
S Q=P^{3} Q^{3} R S ; S R=R S \\ C_{1}=E ; C_{2}=P^{3} Q^{2} ;
C_{3}=P^{6} ; C_{4}=P^{9} Q^{2} ; C_{5}=P^{6} R, Q^{3}, P^{9} Q^{3} R,
Q^{2} R, P^{6} Q, P^{3} Q R \\ C_{6}=P^{9} Q^{2} R, P^{3} Q, Q R,
P^{3} R, P^{9} Q^{3}, P^{6} Q^{3} R ; C_{7}=P^{11} Q, P^{8} Q^{2},
P^{5} Q^{2} R, P^{2} Q^{3} R, P^{7} R, P^{4} Q^{2}, P^{4} Q R, P Q^{3}
;\end{array}
```

Although there is no problem with the LaTeX code itself, I want to
tweak these code as follows, so that it can be used as a GAP [1]
input:

1. Remove the following stuff: `\begin{array}{l}', `\end{array}'.
2. For each word, remove '{' and '}', spaces, and add * between two
characters of it, i.e., `P^{5} Q^{2} R' should be changed to
`P^5*Q^2*R'.
3. Change `C_{digit}` to `Cdigit`, i.e., `C_{5}=P^{6} R' should be
changed to `C5=P^6*R'.
4. Change the first `\\' to new line.
5. Change the first appear of `\\' before the first `C_{digit}` to new line.
6. Change other `\\' to comma.
7. Change `;' to comma.

Any concise tips and tricks to achieve this goal?

[1] https://www.gap-system.org/

Regards
-- 
Assoc. Prof. Hongsheng Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province

[-- Attachment #2: image.png --]
[-- Type: image/png, Size: 151111 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-16  3:25 Convert some Latex expressions in Emacs Hongyi Zhao
@ 2022-05-16  4:49 ` Emanuel Berg
  2022-05-17  0:02 ` Michael Heerdegen
  1 sibling, 0 replies; 12+ messages in thread
From: Emanuel Berg @ 2022-05-16  4:49 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> 1. Remove the following stuff: `\begin{array}{l}',
> `\end{array}'.

Kill manually.

> 2. For each word, remove '{' and '}', spaces, and add *
> between two characters of it, i.e., `P^{5} Q^{2} R' should
> be changed to `P^5*Q^2*R'.

You can do this with `re-search-forward' and regular
expressions that also have subexpressions, you then refer back
to them in `replace-match'. E.g., eval the following

(while (re-search-forward "_\\(.*\\)_" (point-max) t)
  (replace-match "\\\\textit{\\1}") )

To change

  - That ain't _true_!
  - I _know_ right?

into

  - That ain't \textit{true}!
  - I \textit{know} right?

> 4. Change the first `\\' to new line.

If it just happens once, doing it manually is as fast or
faster as any other way, either that or it should be.

> 6. Change other `\\' to comma.

After doing the first manually, `replace-regexp', this
time no need for subexpressions tho.

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-16  3:25 Convert some Latex expressions in Emacs Hongyi Zhao
  2022-05-16  4:49 ` Emanuel Berg
@ 2022-05-17  0:02 ` Michael Heerdegen
  2022-05-17  2:12   ` Hongyi Zhao
  1 sibling, 1 reply; 12+ messages in thread
From: Michael Heerdegen @ 2022-05-17  0:02 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> 1. Remove the following stuff: `\begin{array}{l}', `\end{array}'.
> 2. For each word, remove '{' and '}', spaces, and add * between two
> characters of it, i.e., `P^{5} Q^{2} R' should be changed to
> `P^5*Q^2*R'.
> [...]

If you have some time you might have a look at peg.el (from Gnu Elpa).
PEGs (Parsing Expression Grammars) are a bit more expressive than
regular expressions.  Takes a bit of time to get used to the concept but
if you have to solve such tasks more often it may pay off.

Michael.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17  0:02 ` Michael Heerdegen
@ 2022-05-17  2:12   ` Hongyi Zhao
  2022-05-17  2:22     ` Emanuel Berg
  2022-05-17  3:23     ` Michael Heerdegen
  0 siblings, 2 replies; 12+ messages in thread
From: Hongyi Zhao @ 2022-05-17  2:12 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

On Tue, May 17, 2022 at 8:02 AM Michael Heerdegen
<michael_heerdegen@web.de> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> > 1. Remove the following stuff: `\begin{array}{l}', `\end{array}'.
> > 2. For each word, remove '{' and '}', spaces, and add * between two
> > characters of it, i.e., `P^{5} Q^{2} R' should be changed to
> > `P^5*Q^2*R'.
> > [...]
>
> If you have some time you might have a look at peg.el (from Gnu Elpa).
> PEGs (Parsing Expression Grammars) are a bit more expressive than
> regular expressions.  Takes a bit of time to get used to the concept but
> if you have to solve such tasks more often it may pay off.

I roughly browse the relevant introduction here [1]. It seems that all
Emacs Lisp based regex patterns are more complicated and tedious than
other languages, e.g., python, wolfram, and so on.

[1] https://elpa.gnu.org/packages/peg.html

> Michael.
>
Regards,
Hongsheng



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17  2:12   ` Hongyi Zhao
@ 2022-05-17  2:22     ` Emanuel Berg
  2022-05-17  3:23     ` Michael Heerdegen
  1 sibling, 0 replies; 12+ messages in thread
From: Emanuel Berg @ 2022-05-17  2:22 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

>> If you have some time you might have a look at peg.el (from
>> Gnu Elpa). PEGs (Parsing Expression Grammars) are a bit
>> more expressive than regular expressions. Takes a bit of
>> time to get used to the concept but if you have to solve
>> such tasks more often it may pay off.
>
> I roughly browse the relevant introduction here [1].
> It seems that all Emacs Lisp based regex patterns are more
> complicated and tedious than other languages, e.g., python,
> wolfram, and so on.

It's the same as everywhere. Start appling them on your own
use case and you'll see it isn't difficult at all.

Here is an interesting post BTW:

  https://lists.gnu.org/archive/html/help-gnu-emacs/2022-05/msg00190.html

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17  2:12   ` Hongyi Zhao
  2022-05-17  2:22     ` Emanuel Berg
@ 2022-05-17  3:23     ` Michael Heerdegen
  2022-05-17  3:38       ` Michael Heerdegen
  2022-05-17  3:44       ` Hongyi Zhao
  1 sibling, 2 replies; 12+ messages in thread
From: Michael Heerdegen @ 2022-05-17  3:23 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> I roughly browse the relevant introduction here [1]. It seems that all
> Emacs Lisp based regex patterns are more complicated and tedious than
> other languages, e.g., python, wolfram, and so on.
>
> [1] https://elpa.gnu.org/packages/peg.html

No, that's something different (and a bit more powerful).  Didn't want
to confuse you.

When you do want to use regexps - what's the problem?  You have
incremental regexp based query-replace, symbolic regexps with `rx',
nearly anything one can dream of.

Michael.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17  3:23     ` Michael Heerdegen
@ 2022-05-17  3:38       ` Michael Heerdegen
  2022-05-17  3:44       ` Hongyi Zhao
  1 sibling, 0 replies; 12+ messages in thread
From: Michael Heerdegen @ 2022-05-17  3:38 UTC (permalink / raw)
  To: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> When you do want to use regexps - what's the problem?  You have
> incremental regexp based query-replace, symbolic regexps with `rx',
> nearly anything one can dream of.

Elisp regexps are explained extensively here:

  (info "(elisp) Regular Expressions")

including a symbolic syntax (rx), examples, pitfalls and related tools.

Michael.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17  3:23     ` Michael Heerdegen
  2022-05-17  3:38       ` Michael Heerdegen
@ 2022-05-17  3:44       ` Hongyi Zhao
  2022-05-17  4:46         ` Emanuel Berg
  2022-05-17 23:45         ` Michael Heerdegen
  1 sibling, 2 replies; 12+ messages in thread
From: Hongyi Zhao @ 2022-05-17  3:44 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

On Tue, May 17, 2022 at 11:23 AM Michael Heerdegen
<michael_heerdegen@web.de> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> > I roughly browse the relevant introduction here [1]. It seems that all
> > Emacs Lisp based regex patterns are more complicated and tedious than
> > other languages, e.g., python, wolfram, and so on.
> >
> > [1] https://elpa.gnu.org/packages/peg.html
>
> No, that's something different (and a bit more powerful).  Didn't want
> to confuse you.

I rechecked the description here [1], and still can't find any
difference. Why do you say they are different?

[1] https://www.emacswiki.org/emacs/peg.el

>
> When you do want to use regexps - what's the problem?  You have
> incremental regexp based query-replace, symbolic regexps with `rx',
> nearly anything one can dream of.
>
> Michael.
>
>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17  3:44       ` Hongyi Zhao
@ 2022-05-17  4:46         ` Emanuel Berg
  2022-05-18  0:25           ` Hongyi Zhao
  2022-05-17 23:45         ` Michael Heerdegen
  1 sibling, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2022-05-17  4:46 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> I rechecked the description here [1], and still can't find
> any difference. Why do you say they are different?

Start by solving your problem or situation, any method
will do.

After that, compare packages offering regexp technology.

It's the natural order of things.

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17  3:44       ` Hongyi Zhao
  2022-05-17  4:46         ` Emanuel Berg
@ 2022-05-17 23:45         ` Michael Heerdegen
  2022-05-18  0:23           ` Hongyi Zhao
  1 sibling, 1 reply; 12+ messages in thread
From: Michael Heerdegen @ 2022-05-17 23:45 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> I rechecked the description here [1], and still can't find any
> difference. Why do you say they are different?
>
> [1] https://www.emacswiki.org/emacs/peg.el

PEGs can match more "complicated" things than regexps can,
e.g. mathematical formulas.  Theory here:

 https://en.wikipedia.org/wiki/Parsing_expression_grammar

Unlike regexps, a PEG contains a set of named "rules" (as know from
formal grammars).  These are allowed to be recursive and interdependent.
This also allows to describe a "matcher" in a more structured and
better (human)readable way.

peg.el is implemented in Elisp and has some additional features like
executing Elisp code while matching is performed.

Still, when familiar with regexps, PEGs also look familiar, as you have
already noticed.

But I mentioned this only because I noticed your academical background,
and thought that somebody who likes GAP also might be interested in that
approach.  If you just want to get things done, DON'T look at PEGs, use
regexps (`query-replace-regexp', C-M-%) for now.

Should not be hard to do: removing stuff works by replacing with the
empty string.  You may want to know how to refer to the matched string
and how to use Lisp expressions to calculate parts of the replacement
string, that's explained here:

  (info "(emacs) Regexp Replace")

and the rest should be doable quite trivially.  Or do you have any
concrete questions or problems?  Or should we spell some things out?


Michael.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17 23:45         ` Michael Heerdegen
@ 2022-05-18  0:23           ` Hongyi Zhao
  0 siblings, 0 replies; 12+ messages in thread
From: Hongyi Zhao @ 2022-05-18  0:23 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

On Wed, May 18, 2022 at 7:45 AM Michael Heerdegen
<michael_heerdegen@web.de> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> > I rechecked the description here [1], and still can't find any
> > difference. Why do you say they are different?
> >
> > [1] https://www.emacswiki.org/emacs/peg.el
>
> PEGs can match more "complicated" things than regexps can,
> e.g. mathematical formulas.  Theory here:
>
>  https://en.wikipedia.org/wiki/Parsing_expression_grammar
>
> Unlike regexps, a PEG contains a set of named "rules" (as know from
> formal grammars).  These are allowed to be recursive and interdependent.
> This also allows to describe a "matcher" in a more structured and
> better (human)readable way.
>
> peg.el is implemented in Elisp and has some additional features like
> executing Elisp code while matching is performed.
>
> Still, when familiar with regexps, PEGs also look familiar, as you have
> already noticed.
>
> But I mentioned this only because I noticed your academical background,
> and thought that somebody who likes GAP also might be interested in that
> approach.  If you just want to get things done, DON'T look at PEGs, use
> regexps (`query-replace-regexp', C-M-%) for now.
>
> Should not be hard to do: removing stuff works by replacing with the
> empty string.  You may want to know how to refer to the matched string
> and how to use Lisp expressions to calculate parts of the replacement
> string, that's explained here:
>
>   (info "(emacs) Regexp Replace")
>
> and the rest should be doable quite trivially.  Or do you have any
> concrete questions or problems?  Or should we spell some things out?

Got the idea and the specific practices, thank you for your comments.

>
> Michael.

Best,
Hongyi



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Convert some Latex expressions in Emacs.
  2022-05-17  4:46         ` Emanuel Berg
@ 2022-05-18  0:25           ` Hongyi Zhao
  0 siblings, 0 replies; 12+ messages in thread
From: Hongyi Zhao @ 2022-05-18  0:25 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, May 17, 2022 at 12:46 PM Emanuel Berg <incal@dataswamp.org> wrote:
>
> Hongyi Zhao wrote:
>
> > I rechecked the description here [1], and still can't find
> > any difference. Why do you say they are different?
>
> Start by solving your problem or situation, any method
> will do.
>
> After that, compare packages offering regexp technology.
>
> It's the natural order of things.

Got it. Thank you for your penetrating insights.

> --
> underground experts united
> https://dataswamp.org/~incal
>
>



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-05-18  0:25 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-16  3:25 Convert some Latex expressions in Emacs Hongyi Zhao
2022-05-16  4:49 ` Emanuel Berg
2022-05-17  0:02 ` Michael Heerdegen
2022-05-17  2:12   ` Hongyi Zhao
2022-05-17  2:22     ` Emanuel Berg
2022-05-17  3:23     ` Michael Heerdegen
2022-05-17  3:38       ` Michael Heerdegen
2022-05-17  3:44       ` Hongyi Zhao
2022-05-17  4:46         ` Emanuel Berg
2022-05-18  0:25           ` Hongyi Zhao
2022-05-17 23:45         ` Michael Heerdegen
2022-05-18  0:23           ` Hongyi Zhao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).