Skip to content

Toggle different styles of fraction, arrow etc. support request #2402

@pilgrimlyieu

Description

@pilgrimlyieu

I've checked that the fraction toggle only works for .../... and \frac{...}{...}.

I consider that it should be useful to toggle different styles of fraction such as cycling in \frac{...}{...}, \dfrac{...}{...} and \cfrac{...}{...}.

Besides, this can also apply to arrow toggle, which means we can cycle in ->, \rightarrow, \Rightarrow and so on.

I've tried it through another vim plugin called UltiSnips

snippet code
global

command_mapping = ['🚀', '🚁', '🚂', '🚃', '🚄', '🚅', '🚆', '🚇', '🚈', '🚉', '🚊', '🚋', '🚌', '🚍', '🚎', '🚏']

def choose_next(string, array, length = 0):
    return array[array.index(string) - (length or len(array)) + 1]

def command_cycle(target, commands, bracketnum = 1):
    length = len(commands)
    command_map = command_mapping[:length]
    for i in range(length):
        target = target.replace(commands[i], command_map[i])
    string = list(target)
    depth = brackets = 0
    for i in range(len(string) - 1, -1, -1):
        if string[i] == '}': depth += 1
        elif string[i] == '{': depth -= 1
        elif brackets == bracketnum:
            if not depth:
                try:
                    string[i] = choose_next(string[i], command_map, length)
                    break
                except:
                    pass
        brackets += 0 if depth else 1
    result = ''.join(string)
    for i in range(length):
        result = result.replace(command_map[i], commands[i])
    return result

fractions = ['\\frac', '\\dfrac' ,'\\cfrac']

endglobal

snippet "(\\(frac|dfrac|cfrac)\{.*\}\{.*\})" "Fraction Transformation" r
`!p
snip.rv = command_cycle(match.group(1), fractions, 2)
`
endsnippet

However, this solution is easy-trigger, which means even in case like \frac{...}{...}...{...}<tab> can trigger the transformation.

I've also struggled to use commands like vimtex#delim#get_surrounding to match the brackets via UltiSnips, but cause I'm a new learner so I failed.

I wonder if there is a simple solution that can apply for different cases to achieve my request, Or it can be taken as a feature into later version.

Appreciating your wonderful work!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions