Main

Setup VS Code for Scientific Writing with LaTeX

Use VS Code to write in LaTeX with the LaTeX Workshop extension In this video, we'll see how to setup Visual Studio Code for writing scientific papers in LaTeX. I switched to Visual Studio Code for LaTeX because I wanted to do everything research-related in one editor. Thanks to the LaTeX Workshop extension, it turns out that VS Code is the best LaTeX editor I have used so far. Works on Mac, Windows, and Linux. Supports special features for dealing with bibliographies, BibTeX, equations, syncing between tex source and PDF, etc. Other VS Code extensions also provide Zotero integration, if that is the reference manager that you use. 👍 Please like if you found this video helpful, and subscribe to stay updated with my latest tutorials. 🔔 For written instructions and additional details, check out my blog post: https://vincent.codes.finance/posts/vscode-latex/ 🔖 Chapters: 00:00 Intro 01:07 Installation and setup 03:10 Compile 03:46 Multi-file projects 05:04: LaTeX-PDF sync 06:36 Formatting TeX and BibTex 14:10 Dealing with errors 16:11 Sidebar 17:07 Outro Video links: LaTex Project:https://www.latex-project.org/ LaTeX Workshop: https://github.com/James-Yu/LaTeX-Workshop/wiki latexindent.pl: https://github.com/cmhughes/latexindent.pl More Vincent Codes Finance: ✍🏻 Blog: https://vincent.codes.finance 🐦 X: https://twitter.com/CodesFinance 🧵 Threads: https://www.threads.net/@codesfinance 😺 GitHub: https://github.com/Vincent-Codes-Finance #research #researchtips #latex #quarto #python #vscode #professor #academia #researchscholar #visualstudiocode #writing #scientificwriting #scholar #phd #zotero #bibtex

Vincent Codes Finance

4 days ago

Hello and welcome to Vincent Codes Finance! In this video I'll show you how to set up VS Code for writing in LaTeX. There are already plenty of editors that are specialized for LaTeX, why would I want to use VS Code for that? For me the main reasons are 1) it provides a uniform workspace where I can do both my research and the writing in the same interface; I can do my research coding and the writing in the same software. 2) It is really extensible so there's a lot of extensions that are already
available that will be useful for writing in LaTeX. 3) VS Code has a built-in support for Version Control Systems such as git and GitHub so you can track your changes easily in your code and collaborate with others and finally 4) VS Code is really customizable so I can really set it up the way I want. By the way, if those are topics that you find interesting consider liking so that others also get to discover this video and think about subscribing so that you get notified of my future videos. F
or this video, I'll assume that you have a working knowledge of LaTeX and that you have a working LaTeX distribution on your computer. If you don't well you can head to the LaTeX project website and from there you can download the distribution for your system. Here's my VS Code interface the first thing I want to do because writing is going to be a somewhat different task than just coding is I'll create a new profile that I'll use specifically for writing in LaTeX. That way I'll be able to confi
gure all my VS Code for tasks that are specific to writing so in order to do that I will come here into manage profile then I will create a new profile and I'll create just an empty one, I won't copy for from anything. I'll just call it LaTeX and finally I'll choose an icon that is representative of writing, maybe some coffee. When I'm ready I'll click create. Now I've got a new profile, first thing I'll do is I'll change the theme so that it is a bit lighter, I like to write with a lighter them
e so this will be good and then I will install the only extension that I actually need to run LaTeX on my computer which is LaTeX Workshop. All I have to do is search for LaTeX and then not use the first one but choose LaTeX Workshop and I'll install this one. There are a few others that you can look into but this is the only one that's actually needed. Once it's installed I'll get an extra icon for TeX so this is where all the features will live but I won't even need to look at these features d
irectly most of the time, all I have to do is open my paper. Here I've got a really simple paper, it is pretty much like a a basic paper. It has one section here, maybe I'll include a second section. So I've got two sections in separate files so I have all of this. Now all I have to do to compile my LaTeX is save; it will actually run compile. I don't see it yet but I can just open the preview panel to have a look at my PDF directly here in my VS Code. So now my paper is compiled. I could easily
go to the section, with F12 I can go to the definition. All I have to do is put my cursor here, then FN F12. I can make use of all these VS Code features to go to definitions and navigate in my paper. This is just random text but I can still add some text here if I save, the paper will recompile automatically. So how did LaTeX Workshop know that once I've saved this file the other one has to be updated? And it didn't compile this file it compiled the root file. Well it does follow a few rules t
o find your root file so you can actually explicitly set it in your files if you want, but by default what it's going to do is look into the root of your directory and try to find a .tex file that has either a documentclass here or at least a begin document tag so that it is sure that this file is meant to create a PDF. Once it's found that file well it assumes that all the other files are linked to that one. One thing that can get really confusing when you write a long paper in LaTeX is findin
g your way to your different Tech files and the corresponding place in your PDF file fortunately LaTeX Workshop supports SyncTeX which is a mechanism for navigating and searching for links between different locations. For example here if I just click on my abstract and do command option J it will highlight in my paper, in my PDF, the location for this part. I can also do the reverse where I can just say go here in this introduction the part here where is it in my paper well then it's just comman
d click on Mac or control click on Windows and it will take you exactly where you want to be. Another useful thing with this SyncTeX is when you have a links so for example here I'm in my second section if I add a reference to my introduction - my first section - here in my paper and I compile this I'll see that I've got the number, so the reference to the section that was added here. But now if I over over that reference I can click on view on PDF and it will just highlight the part that I'm re
ferencing here. Another really really nice feature that I like of VS Code is formatting so VS Code supports a lot of formatter I've been using that like crazy for my code it's been a life changing experience now my code is tidy not so much for my LaTeX code it used to be super super messy but now with LaTeX Workshop in VS Code I can actually format my LaTeX code as well and in order to do that actually LaTeX Workshop doesn't include the formatting itself it uses a thirdparty software called LaTe
X indent, so you will have to install it if you want to use this feature in order to use LaTeX indent follow the link in the description and then look at how you can install it for your system and then make sure that it is in your path. So for me I'm on Mac I looked at the Mac section of the appendix which was here and then I just use homebrew so I copied this bit here in my terminal to use homebrew to install it and after that everything worked. Once you've got that all installed by default LaT
eX Workshop will format your code when you want it for that you'll need to set it as your default formatter so let's try a simple example so I'll just write a just an extension with alignment so for that I can actually use the snipet BAL all in caps when I type enter it will create my begin align and end align so that's another nice feature of LaTeX workshop. Look into it I won't list them all there's tons of Snippets there that they have that are installed by default so that you can really be m
ore productive when using LaTeX. Okay so now I've got my align here I'll just write a simple equation so f of x Square something like that now if I save I'll see that my equation has been added in this page here so now I've got my equation if I actually off over this LaTeX Workshop will provide a preview of that equation so I don't even have to save and compile before I can actually look and preview my equation and now once I've done that if I want I could format my document. The thing to do is
to just go into format it might ask you if it's not set up to set a LaTeX Workshop as your default formatter for me it was already configured so once you got that you see everything lines up nicely so it will format a lot of things and it is super super useful. Another part of my LaTeX coding environment that tends to get really really messy are my BibTeX files. I sometimes use Zotero to manage my libraries but when I'm in a rush editing I'll often just copy the BibTeX entrie from Google Scholar
or another source like that, put them in my BibTeX file and then they end up all over the place, formatted all differently. It becomes really really messy if I go here this is what kind of one of these files might look look like right so it's not too bad but you see like they're not in alphabetical order, everything is not that good.. If I just format it now it's all lined up it's all good. What I really really want to add that I find super super useful is another feature that you need to activ
ate in your settings so for this feature just open the command palette and look for user settings Json or workspace workspace setting depending on which one you want to set up so for me it will be the users settings so this is the settings that are specific to my profile so what I'll want to add here is a setting to tell VS Code and LaTeX Workshop to not only align and format my bib entry but also sort them by alphabetical order. If you go on the LaTeX Workshop website the documentation is quite
massive there are tons of options there that you can set manually LaTeX workshops provides three commands for formatting BibTeX file. One is to sort the file the other one is to align the fields and the third common is to perform both at once. Which one gets called when you format your BibTeX file depends on your settings. What I will do is I will add this entry here and I will set it to true and now when I do that if I come here and format my document now it sorted everything by alphabetical o
rder. I enjoy formatting so much that I actually recommend setting it so that it formats all your document automatically on save. In order to do that you go into your settings here and then you can look for format on save and here I will activate format on save and now whenever I save my document it will get reformatted. By the way if you're using Zotero as your reference managers there are many VS Code extensions to interact with Zotero directly from VS Code so you can actually query your libra
ry from VS Code if you use one of those. I showed you earlier how you can just hover over equations to get a preview of that equation that's not the only preview that is available in VS Code for example if I'm citing a paper I might open my brackets like this and LaTeX Workshop will detect all the entries in my BibTeX file so I'll have a picker here so I'll pick the first one here and now I can actually hover on this and see what paper this is referencing and if I save again everything gets form
atted I can look at my preview and now I've got a reference bibliography that's there the reference is there it is the one that was being previewed here. Now a really painful part of dealing with LaTeX is dealing with errors so for example here instead of having sum maybe I had a typo here and I used a keyword that just doesn't exist if I try to compile I will get an error message so I can actually open the compiler log to see the actual error. That's what I would do in my other editors but in V
S Code LaTeX Workshop will actually try to highlight the part the error is coming from and will tell you the error so here would tell you there is an undefined control sequence here, there are no quick fixes available for that but still it will tell you there is an error here. If you're stuck why one thing that I really really enjoy using with VS Code is GitHub Copilot so if you install the Copilot So now I've got my error I could actually highlight it here maybe ask Copilot with the inline chat
so I'm getting an error can you find a typo which is usually my what I tend to do fix the error I would replace the undefined control sequence some with the correct LaTeX command sum in both instances okay so that's really nice I can accept this change it will actually fix it for me and now I save and my code is fixed. Finally if you want to see more features well you can go here on the TeX on the toolbar and then you'll see a few commands to build or clean your files or to just view the PDF ei
ther in VS Code or in an external viewer which will open preview on my Mac or you can look at different things so you can come here to sort your BibTeX you can look at different settings for SyncTeX you can view your log messages you can also come here and see the structure of your file so you can browse the structure of your file directly and you can also see kind of snippet so the symbol viewer and there's also one for graphs if you if you want so that's all you need to run LaTeX on VS Code th
ere are a few other extensions that I find useful so for example I use the grammarly extension for syntax and kind of writing recommendations there are other spell Checkers and grammar Checkers available out there and I really recommend if you're not doing that already to look into using Source control so get and get up for example to keep track of changes in your Tech files thank you for listening if you enjoyed this video please like and also consider subscribing to the channel so that you get
notified of my future videos

Comments