Sublime Forum

Different Syntax modes in a same document

#1

Hi,

I use sublime text 3 for latex. In the latex documents, I have some parts containing C++ code
(defined between \begin{lstlisting} and \end{lstlisting}).
How can I explain sublime text that between these two delimiters, it is C++ code ?
I mainly want to be able to use AstyleFormatter plugin on C++ code parts.
It seems that it is possible with tmLanguage but it is not clear for me.

Thanks in advance,
Mathieu

0 Likes

#2

Can’t you just select the region of C++ code and choose SublimeAStyleFormatter: Format Current Selection from the command palette?

0 Likes

#3

Yes it’s possible, but you will have to modify the latex syntax.

Sublime Text 3 doesn’t use tmLanguage anymore , but a .sublime-syntax format.
I don’t have time to give you a full response now, so I’ll just leave the doc: https://www.sublimetext.com/docs/3/syntax.html

I’ll come back with an example later.

1 Like

#4

Latex syntax has been re-written but not yet merged, compare:

I guess @randy3k could answer that question!

0 Likes

#5

This is already a feature in the pull request @rppn referenced. You can see in a screenshot near the end of it showing that it works for Python code, for example. However, it needs more polishing, which is why it hasn’t been merged yet.

0 Likes

#6

Hi,

Thanks a lot for all your answers.
However, I wonder whether changing the highlighting for the C code (even if it would be much appreciated)
would allow me to apply plugins for C code (like AstyleFormatter for instance) in a latex file.
Is that true? In this case, I can wait for the merge of the latex syntax or try to write a .sublime-syntax format as suggested by @gwenzek .
Otherwise, is there some tricks to manually specify that some parts of a file must be considered with other such as:
latex code %! begin of language C C code %! end of language C latex code

Mathieu

@315234 : the problem is that, without changing the syntax of the file (in the command palette, set syntax C), the plugin AstyleFormatter is not even available. Hence, each time I need to format a small example of C code, I need to change the syntax of the file to C, then apply the plugin and change back the syntax to latex.

0 Likes

#7

If you choose to edit the Latex syntax, you can have a look at my Markdeep syntax.

In Markdeep you can insert code block with ~~~~ <language-name>, ended by ~~~~.
The important part of the syntax code is the

push:
  - meta_content_scope: markup.raw.block.markdeep
  - include: "scope:source.python"
with_prototype:
  - match: '(?=(~{4,}|`{4,}))'
    pop: true

The include allows to use another syntax, and the with_prototype allows to pop out of the syntax you included.
It’s important to use a look-ahead regex (with the (?=...)) allowing to pop until we are out of the python syntax.

1 Like

#8

To modify the current syntax file you can just install PackageResourceViewer and open the latex syntax afterwards search for python (which is supported) and add the C++ syntax similar.

Or you download the new latex syntax and move the LaTeX folder into your package directory.

Afterwards just add a comment to your document:

\begin{lstlisting} % C++
int foo = 5;
\end{lstlisting}
0 Likes

#9

Hi,

Thanks for your answers.
I have downloaded the latex directory and put it in
~/.config/sublime-text-3/Packages
(I have first renamed the existing Latex directory).
However , it doesn-t work, C++ is not taken into account
(I tried java and python and it works for these languages).

Mathieu

0 Likes

#10

You must download the LaTeX folder from the git branch latex. The current syntax only supports python and java, the new one will support many languages.

0 Likes

#11

It works! In fact, the problem was that I renamed the old latex directory without moving from the package directory. Thus, I had two different “latex syntax” in ST3: one for the new and one for the old one.

Thanks a lot everybody.

0 Likes

#12

Hi,

Everything is ok, except that I need to choose “set syntax latex” in the command palette for the C++ code part to be taken into account, each time I open my latex file. It seems that is it not initially considered.
Is that normal?

Mathieu

0 Likes

#13

No, that is not normal. You could try to open the ST console ctrl+` and write view.settings().get("syntax") when the C++ is not available and when its available. I would assume you get different syntax files.

0 Likes

#14

Hi,

You were right. Initially, the syntax file was LaTeX+.
After several attempts to modify the Latex+.sublime-syntax file,
I copy/past the Latex.sublime-syntax content into the Latex+.sublime-syntax
(I only change the name line 4).
Now, everything works.
Thanks a lot!!!

0 Likes