Make new command in .ipynb markdown in VSCode - visual-studio-code

I am trying to create new commands for markdown in an ipython notebook file in VSCode, but am having trouble doing so
This post shows an example which (kinda) works in jupyter notebook:
$\newcommand{\vect}[1]{{\mathbf{\boldsymbol{{#1}}}}}$
This is the vector $\vect{x}$.
But pasting this exact code in VSCode, I get the error:
ParseError: KaTeX parse error: Undefined control sequence: \vect at position 1: \vect{x}.
So it seems the new command does not get created. Am grateful for any solution

Issue 125425 opened by Chandresh Pant and mentioned in the comments seems to be solved for VSCode 1.69 (June 2022)
See PR 148006 and commit acb156d:
In order to make macros defined by the author persistent between KaTeX elements, we need to pass one shared macros object into every call to the renderer.
KaTeX will insert macros into that object and since it continues to exist between calls, macros will persist.
See KaTeX docs.

Try the Markdown + Math extension by Stefan Goessner which supports macros. It works really well on my setup.
We can also define macros in the user settings, e.g.
"mdmath.macros": {
"\\vect" "{\\mathbf{\\boldsymbol{{#1}}}}"
}
or in a separate json file as follows.
"mdmath.macroFile": "/path/to/macros.json"

Related

Markdown code block highlight consistency - what is "s" alias

The built-in markdown editor has a "live-preview" for code blocks without open a side previewer.
I was looking for a list of supported language highlight, which as I understand is defined by highlight.js supported language.
I need to add some "ssh config files" in my markdown, but
when I use properties language tag, only side previewer shows the highlight, not the editor itself
when I try with a weird "s" language alias, the editor can detect the comments and non-alphabetic characters. But the side previewer shows nothing...
VSCode screenshot:
My questions:
"s" is not defined as an alias in highlight.js, then how come the editor can detect the language?
Why the editor and previewer do not have the same highlight behavior?
I prefer to have the correct highlight in editor, which language/alias should I use in place of "properties"?
Summarizing previous comments and answers here:
VScode markdown "in-editor" code highlight use "markdown-basic" extension, with a list of supported languages in this file
The "alias" for different languages are "hidden" in the regex of each block definition. for example for R language the alias is R|r|s|S|Rprofile|\\{\\.r.+?\\}. (I extract a complete list of aliases from the source codes and it is shown below)
More specifically for what I want, i.e. key-value pair config file equivalent to properties in highlight.js, there is no exact the same in "markdown-basic", so I will use conf.
Developer: Inspect Editor Tokens and Scopes helps a lot when you see a unfamiliar alias and want to know what it is
List of code block languages aliases for "markdown-basic":
bat|batch
bibtex
c|h
clj|cljs|clojure
coffee|Cakefile|coffee.erb
COMMIT_EDITMSG|MERGE_MSG
cpp|c\\+\\+|cxx
cs|csharp|c#
css|css.erb
dart
dockerfile|Dockerfile
elixir
erlang
fs|fsharp|f#
git-rebase-todo
go|golang
groovy|gvy
handlebars|hbs
html|htm|shtml|xhtml|inc|tmpl|tpl
ini|conf
jade|pug
java|bsh
js|jsx|javascript|es6|mjs|cjs|\\{\\.js.+?\\}
json|json5|sublime-settings|sublime-menu|sublime-keymap|sublime-mousemap|sublime-theme|sublime-build|sublime-project|sublime-completions
jsonc
latex|tex
less
log
lua
Makefile|makefile|GNUmakefile|OCamlMakefile
markdown|md
objectivec|objective-c|mm|objc|obj-c|m|h
patch|diff|rej
perl|pl|pm|pod|t|PL|psgi|vcl
perl6|p6|pl6|pm6|nqp
php|php3|php4|php5|phpt|phtml|aw|ctp
powershell|ps1|psm1|psd1
python|py|py3|rpy|pyw|cpy|SConstruct|Sconstruct|sconstruct|SConscript|gyp|gypi|\\{\\.python.+?\\}
R|r|s|S|Rprofile|\\{\\.r.+?\\}
re
regexp
ruby|rb|rbx|rjs|Rakefile|rake|cgi|fcgi|gemspec|irbrc|Capfile|ru|prawn|Cheffile|Gemfile|Guardfile|Hobofile|Vagrantfile|Appraisals|Rantfile|Berksfile|Berksfile.lock|Thorfile|Puppetfile
rust|rs|\\{\\.rust.+?\\}
scala|sbt
scss
shell|sh|bash|zsh|bashrc|bash_profile|bash_login|profile|bash_logout|.textmate_init|\\{\\.bash.+?\\}
sql|ddl|dml
swift
tsx
typescript|ts
vb
xml|xsd|tld|jsp|pt|cpt|dtml|rss|opml
xsl|xslt
yaml|yml
I don't believe VSCode's native syntax highlighting has anything to do with highlight.js. It is provided by language-specific extensions, some of which ship with the editor.
In this case, VSCode is using its built-in R mode for that code block. You can see the language being used via Developer: Inspect Editor Tokens and Scopes in the command palette. R is an open-source implementation of the language S, so this makes sense.
The Markdown preview, on the other hand, may well use highlight.js and you are right that s is meaningless there.
I don't see a good language code to use in your code fence, but will update this answer if I find one.

Variable name autocomplete for VSCode Language Extension (GameMaker / GML files)?

I'm editing GML files (GameMaker Studio) in VSCode. There's a wonderful plugin, GML Support which adds autocomplete for inbuilt GML functions and instances variables along with a bunch of other cool things.
However, VSCode doesn't seem to recognise local variables in GML (see screen grab below. Dot notation works fine)
I had a look at the VSCode's Programmatic Language Extension for variable name auto-completion but still don't get how I could register the variable declaration (i.e. var fooBar = 23;) with VSCode's Language Server.
Ideally, I'd like the Language Server to respect variable scope for GML files:
global variables - any var declarations for files under script folder
any local variable declarations - all var declarations in the surrounding {...}
What would be the easiest way to add variable name completion as described above?
Thanks in advance!
Edit: looked at vscode-python to see how registerCompletionItemProvider (based on VSCode Language Extension doco) could be used. Unfortunately, still not clear to me as vscode-python seem to rely on Jedi to provide symbols?
So any points appreciated!
If you want to enable simple auto-completion, you can add the following to your settings.json (Command Palette ➜ Open Settings (JSON)):
"[gml-gms81]": { "editor.quickSuggestions": true },
"[gml-gms1]": { "editor.quickSuggestions": true },
"[gml-gms2]": { "editor.quickSuggestions": true },
which works for a workaround:
For a proper solution, well, you'll need to use the registerCompletionItemProvider and index the file on demand or as you go.
The official example demonstrates the use.
For intricacies of processing GML syntax, you can peck at the code in the Ace-based external editor that I made. Processing variable definitions specifically requires you to skip over strings, comments, and loop over values (var name[=value][, name2[=value2]]) with relative degree of confidence (which can be accomplished through a balanced parser).

Why does VS Code break my Markdown fenced code blocks?

I'm creating a markdown document with some CSharp code blocks. Here's a sample:
Next, it feeds the strings to the regular expression matcher to produce a sequence of matches.
```csharp
let patternMatch = azimuthEncoderRegex.Match(message)
```
In the editor, this seems to be working nicely, like so:
As you can see, the code is formatted as expected and shows up correctly formatted in the preview window (not shown).
Now, when I save my file, the above text instantly changes to this:
If I use search-and-replace to change all the code specifiers back, the same thing happens. This breaks the code formatting!! The entire file is also re-flowed to remove all the line breaks I put in (that may be a clue).
UPDATE: I noticed that all of the reference-style hyperlinks were also removed from the end of the document, causing data loss.
WTF? Why is VS-Code doing this? I've tried disabling the Markdown extensions and the same thing happens. Any ideas, please?
Resolved by a change in settings.json for VS Code:
{
"pandocFormat.command": "pandoc --standalone --atx-headers --wrap=auto --columns=80 -f markdown-auto_identifiers -t markdown-simple_tables-multiline_tables-grid_tables-auto_identifiers-fenced_code_attributes --reference-links"
}
Thanks and credit to monofon (the author of the VS Code extension, based on Pandoc) for steering me to this solution.

Sublime Text 3: Auto-Complete uses incorrect syntax for for loop

With sublime text 3, the autocomplete when typing "for" and hitting tab gives you:
for x in xrange(1,10):
pass
However, this is not a valid statement for python 3. I've tried creating a new build system using the following:
{
"cmd": ["c:/Python37/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
the auto-complete for for still gives the wrong syntax. any advice?
The short version is that the sublime-build and sublime-snippet files that ship with Sublime in support of Python target Python version 2 and not Python version 3. I don't know if that's just due to that being what was used initially or if it's being done on purpose, though.
In Sublime, resources are generally related to a particular language based on the scope provided by the syntax definition. So for example snippets for Python are associated with source.python, your example build file uses that scope to know that it applies to Python files, and so on. As such, no matter what build you happen to be using, that has no effect on the snippets that are being offered.
By way of example, if you use the View Package File command from the command palette and enter the text python for snippet, the list of package resources will filter to Python/Snippets/for.sublime-snippet; pressing Enter to view that resource shows this:
<snippet>
<tabTrigger>for</tabTrigger>
<scope>source.python</scope>
<description>For Loop</description>
<content><![CDATA[
for ${1:x} in ${2:xrange(1,10)}:
${0:pass}
]]></content>
</snippet>
Here the tabTrigger specifies how the snippet inserts, scope controls where it inserts and content controls what it is inserts. Thus, in order to change it to support Python 3, you need to either create your own snippet or modify the existing one.
An issue with creating your own snippet is that it will be added to the list of snippets including the offending one, which allows it to possibly still trigger when you don't expect it to. There is also no general purposes "easy" way to disable individual snippets.
As such, generally the best course of action would be to use the PackageResourceViewer package. Install it, select PackageResourceViewer: Open Resource from the command palette, then select the same file as outlined above and modify the content of the snippet (e.g. replace xrange with range) and save the file.
That will get Sublime to replace the existing snippet with your edited version, so that it takes the place of the existing one and works the way you want.

Visual Code, Fold Comments

Visual Studio Code 1.24.1
While I was working on something today. It prompted me to do an update which I did (Update was to 1.24.1). I'm not sure if I hit a shortcut accidentally at about this same time or if this was caused by the update.
But I seem to no longer be able to use comments as a fold point.
However again, I'm not sure if I hit a shortcut of some sort, or if this was caused by the patch.
and my googlefu did not help me find an answer for visual studio code. Found many old topics about visual studio (prof not code) and for other editors. but couldn't find a topic specific to VSC.
I liked to use comments as fold points \ section headers.
Example of comments I used to use as fold points
Is this a bug in VSC 1.24.1 or did I hit a shortcut I'm unaware of?
VS Code has a method of marking the start and end of a region that can be folded and expanded easily. And also provide it a name/short description that'll always be visible.
I've tried this on Javascript code and I believe it'll work on any language in VS Code. And you can have anything in between the start and end of region - comments, few lines or code, entire functions.
In the absence of proper code folding, this is a pretty good work around that'll work for all languages on VS Code.
//#region <REGION NAME>
< You can put any type of code in here - some lines of code, functions or single or multi-line comments.
//#endregion
For python, simply omit the // in the demarcation lines, since # is a valid comment indicator:
#region <REGION NAME>
...
# any code goes here, including multiple comment lines
...
#endregion
A kind of hack I found for react is using empty tags for example
<>
{/*
Your commented code
*/}
</>
This allows you to fold the commented code between the empty tags. You can go a step further and add regions as mentioned in the other answer to add some kind of description to it