1

I want to add global macros to my TeX editor. By this, I mean something like this: every time I write \section{x}, it automatically adds \label{sec:x} after \section{x}.

Is it possible on Texmaker? If not, is there another Windows``TeX editor which allows these kinds of things?

Thanks in advance

5
  • 2
    I wouldn't do this, section titles can contain formatting that doesn't work in labels, and a major idea of \label is that it is a stable internal identifier that you can use that does not change even if you renumber or edit the section title. Your editor macro may seem to save a fraction of time to type \label{foo} but if you then have to edit the whole document to change every \ref when you edit a title, it is time lost. Sep 26, 2023 at 9:18
  • I basically want to add \label{thm:}, \label{prop:} \label{dfn:} automatically each time I add any of these environments. This does save time. I can then manually insert an internal name.
    – omar
    Sep 26, 2023 at 9:22
  • that's reasonable (and what emacs editor does by default) but is not what you asked for (or what you got as an answer) Sep 26, 2023 at 9:24
  • When I asked my question I had in mind the snippet below. I still want to use it.
    – omar
    Sep 26, 2023 at 9:26
  • 1
    @omar For texstudio (originally a texmaker fork, so very similar in usage) you can use github.com/texstudio-org/texstudio/wiki/… Sep 26, 2023 at 10:51

4 Answers 4

3

Solution with VS Code

You can use the user snippet. In the specific case as you have said, you can write a snippet as follows:

  "section":{
    "prefix": "sec",
    "body": "\\section{$1}\\label{sec:$1}\n\n",
    "description": "Insert Section Command",
  }

into latex.json. Then insert sec to trigger the snippet:

enter image description here

But for some long section title, it's inappropriate to set the label the same as section name. So it's better to substitute the second $1 with $2. This way one can give section name and label name separately.

4
  • 1
    hmm \section{I \emph{wonder} if this is a good idea:-)} Sep 26, 2023 at 9:20
  • @Stephen Thank you very much. It work perfectly !
    – omar
    Sep 26, 2023 at 9:36
  • @omar See my updated version.
    – Stephen
    Sep 26, 2023 at 10:25
  • This solution given can be used, or not, so this is an excellent tip even if I have decided to use VSCodium. ;-)
    – projetmbc
    Sep 27, 2023 at 8:41
3

In emacs (auctex) this is set up by default.

enter image description here

C-c C-s then triggers a prompt

enter image description here

hit <return>

prompts for title

enter image description here

it then prompts for (optional) label, discarding it if you just hit return)

enter image description here

So producing

enter image description here

From the keystrokes

C-cC-s<return>hello world<return>foo<return>

1
  • Thank you very much for your answer
    – omar
    Sep 26, 2023 at 22:09
1

Gummi is a simple LateX editor but in menu EditPreferences allow to define code snippets as this:

\begin{figure}[htp]
\centering
\includegraphics[scale=${1:1.0}]{$2}
\caption{$3}
\label{$4}
\end{figure}
$0

Typing anywhere imgTab in the document, this code will be pasted As is, except the variables $0 to $4. The variable $1 have a default value, so ${1:1.0} will appear as 1.0 and selected automatically, while the othes will be empty ({$2} will appear as {}, and so on). As you start with the value of $1 selected, typing some like 0.3Tab will change the default image scale and jump to $2 position. So, to complete the whole snippet, you can type some like example-image Tab My caption Tab mylabelTab I am now in main text ...

The final resul will be:

\begin{figure}[htp]
\centering
\includegraphics[scale=0.3]{example-image}
\caption{My caption}
\label{mylabel}
\end{figure}
I am now in main text ...▎

Note: the ▎symbol represent the final cursor, not any real character. Do not copy this chunk of code in a document compiled with pdflatex, or you will obtain the LaTeX Error: Unicode character ▎ (U+258E) not set up for use with LaTeX.

Of course, you can edit the snippets to add for instance fig: to labels of figures automatically, or make your own new snippet of whatever code and call it with whateverTab.

Some predefined snippets already make label types added automatically. For example secTab will call to:

\section{${1:section name}}
\label{sec:${2}}
$0
% section $2 (end)

Therefore, sec Tab mysectionTab fooTab My nice text ... will produce:

\section{mysection}
\label{sec:foo}
My nice text ... 
% section foo (end)
1
  • Thank you very much for your answer
    – omar
    Sep 26, 2023 at 22:09
1

One could also use AutoHotKey for this purpose. Then you are independent of editors, and can even sync the script to different computers to have the same shortcuts everywhere in every editor. To produce a section with label command, a possible solution would be:

:*:c#::\chapter{{} {}} {RETURN}\label{{}sec: {}}

With this, you can type

c#

, which will be replaced to

\chapter{ } 
\label{sec: }

I do a similar thing for references in texstudio, were I also trigger the autocomplete function of texstudio by simulating further keyboard input. To insert a symbol with glossaries package, the following code creates the command to reference to the symbol and start the auto complete dropdown when I type sym#:

:*:sym#::\gls{{}sym:{}}{LEFT}{Ctrl down}{Space}{Ctrl up}

Produces:

\gls{sym:}

In TeXstudio, it looks like this: Symbol insert

This could be adapted to all other references, like bibliographies or your other labels.

I believe all this is possible in TeXstudio (a fork of TeXmaker) with its built in scripting language, but I rarely use this.

Edit: Actually, I found an example script for TeXstudio on their script gallery. You can find it here: https://github.com/texstudio-org/texstudio-macro/blob/555dffe7aef3d0433fdcf1784b7ef057c4ee8ec5/automatedTextmanipulation/autoLabel.txsMacro

This does exactly what you asked for, but I did not test it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .