abc {} abc
* Arjen Markus
| I am a bit puzzled about the following:
| set a "\\abc"
| regexp -inline {(^[a-z]+)|([^\\][a-z]+)} $a
(^[a-z]+) lower-case chars at beginning of string, which "\abc" is not
OR
([^\\][a-z]+) anything not-backslash followed by lower-case chars, which "\abc" is: the 'a' qualifies as not-backslash, the "bc" as lower-case
chars.
| I tried to figure out a way to identify words that are either at the
| start of a line or are NOT preceded by a backslash - a first attempt
| to manipulate some Latex source.
| Does anybody know what is going wrong? Most likely something very
| obvious, but I do not see what.
I think you missed some "at-beginning-of-word"-modifier for the backslash.
HTH
R'
On 13/10/2022 13:31, Arjen Markus wrote:
But why the {-1 -1} indices? That does not make sense to me.From the regexp man page: "if a particular subexpression in exp does
not match the string (e.g. because it was in a portion of the expression
that was not matched), then the corresponding subMatchVar will be set to
"-1 -1" if -indices has been specified"
Schelte.Ah, thanks, that makes sense, I think.
But why the {-1 -1} indices? That does not make sense to me.
words that are either at the start of a line
or are NOT preceded by a backslash
On Thu, 13 Oct 2022 04:04:55 -0700 (PDT), Arjen Markus wrote:
words that are either at the start of a lineI find your specification confusing.
or are NOT preceded by a backslash
What if a word is both at the start of a line
and preceded by a backslash? Is it acceptable?
Your specification is not very clear.
On Thursday, October 13, 2022 at 6:52:26 PM UTC+2, Luc wrote:with a backslash).
On Thu, 13 Oct 2022 04:04:55 -0700 (PDT), Arjen Markus wrote:
words that are either at the start of a lineI find your specification confusing.
or are NOT preceded by a backslash
What if a word is both at the start of a lineI'd guess OR is not XOR and "either" indicates XOR ;-)
and preceded by a backslash? Is it acceptable?
Your specification is not very clear.
But I also guess that "either" was unintended and that the "start of a line" just came from Tcl's regexp not supporting negative look-behind (note that words at the beginning of a line with normalized EOL style are always preceded with \n and never
Given the spec, I would have assumed to see [regexp -line {(?:^|[^a-z\\])([a-z]+)}] (untested!).Thanks for the suggestions and explanations, everyone. The thing I am after is:
* The first (non-reporting) group matches at the begin of a line or anything but backslash (but that "anything" should also _not match_ anything we need).
* If you can guarantee that the first word never starts at the beginning of the string (e.g. because of "\documentclass"), then you could drop that ^ along with -line, because [^a-z\\] also matches the line break.
* With -all -indices -inline you would use every other list entry.
* Also, I guess [a-z] was really just a start (but \w is probably too much).
HTH
Martin
- A simple formula might read: klrear = 1.0 + a \times salinity
- I want to change that to: \texit{klrear} = 1.0 + a \cdot \textit{salinity}, because that looks prettier.
Thanks for the suggestions and explanations, everyone. The thing I am after is:
- I have formulas in tex files (Latex) and these can contain words like "salinity".
- A simple formula might read: klrear = 1.0 + a \times salinity
- I want to change that to: \texit{klrear} = 1.0 + a \cdot \textit{salinity}, because that looks prettier.
- I will probably need to do the transformation in stages, but I do not want "\times}" to change into"\\textit{times}", hence my attempt above.
klrear = 1.0 + a \times salinityklrear = 1.0 + a \times salinity
klrear = 1.0 + a \times salinity
klrear = 1.0 + a \times salinity
}
On 14/10/2022 08:22, Arjen Markus wrote:
- A simple formula might read: klrear = 1.0 + a \times salinityYou didn't mention changing "\times" into "\cdot" before. But how about
- I want to change that to: \texit{klrear} = 1.0 + a \cdot \textit{salinity}, because that looks prettier.
this for the other requirements?
regsub -all {([^\\]|^)(\m[a-z]+\M)} $str {\1\textit{\2}}
This will also change the "a" into "\textit{a}". If you don't want
single letters to be changed, use this instead:
regsub -all {([^\\]|^)(\m[a-z]{2,}\M)} $str {\1\textit{\2}}
Schelte.That change is irrelevant to my earlier question, the main thing is to separate "bare" words and Latex macros. I just mention the replacement because that will be part of the final step.
That change is irrelevant to my earlier question, the main thing is to separate "bare" words and Latex macros. I just mention the replacement because that will be part of the final step.
On 10/14/2022 5:26 AM, Arjen Markus wrote:
That change is irrelevant to my earlier question, the main thing is to separate "bare" words and Latex macros. I just mention the replacement because that will be part of the final step.
It sounds like you will accept a non-regexp solution. Here is one:
set line {klrear = 1.0 + a \times salinity}
proc latex_it {line} {
foreach w [split $line " "] {
if {$w eq ""} {
# skipping empty spaces
# you can include it if you want to preserve spacing
continue
} elseif {[string is double -strict $w]} {
append out "$w "
} elseif {$w in {= - + /}} {
# add more operators
append out "$w "
} elseif {[string index $w 0] eq "\\"} {
# add more "special" operators
switch -exact -- $w {
\\times { append out "\\cdot " }
default { append out "$w " }
}
} else {
append out "\\textit\{$w\} "
}
}
return $out
}
proc latex_it {line} {
foreach w [split $line " "] {
# You might want to add \t (tab character) to the split char string.
Also, don't call latex_it with empty line or with line that consists of space only - or set out "" initially.
On 10/14/2022 11:46 AM, Robert Heller wrote:
proc latex_it {line} {
foreach w [split $line " "] {
# You might want to add \t (tab character) to the split char string.
Good catch! You could also handle it separately in an if-statement if
you want to preserve it.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 24:10:49 |
| Calls: | 12,105 |
| Calls today: | 5 |
| Files: | 15,006 |
| Messages: | 6,518,164 |