4

I am writing an article in TeXstudio and want to include an index that contains (many) selected keywords that have already been written. To do so, I use the imakeidx package. Is there a possibility to define a shortcut or a keyboard hotkey such that I can go through my document, mark the respective keyword and press the shortkey instead of copying \index{keyword} several times? In the optimal case, it just works like the italics command Ctrl+I that can also be put around selected words afterwards.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}
\makeindex

\begin{document}

\section{Introduction}
In this example several keywords\index{keywords} will be used 
which are important and deserve to appear in the Index\index{Index}.

Terms like generate\index{generate} and some\index{others} will 
also show up. 

\printindex

\end{document}
doncherry
  • 54,637
  • This question is closely related to my previous question "Is there a shortcut in Overleaf for indexing selected words?". However, it seems that customizing shortkeys in Overleaf does not work, so I switched to TeXStudio. – user209774 Mar 29 '20 at 12:54

1 Answers1

2

You can define a macro as described in Defining new shortcuts in TexStudio, except that you'll need the script type. A quick and dirty script for your task could be this:

%SCRIPT
editor.copy();
editor.paste();
editor.insertText("\\index\{");
editor.paste();
editor.insertText("\}");

Note that this will overwrite whatever currently is in your clipboard. (And you have to select the word you want to add to your index before running the script.)

To assign a keyboard shortcut to your script, either follow the instructions in the linked question or use the Shortcut field, which didn't exist yet when that answer was posted. More info in the manual, section 4.5 Personal macros.

doncherry
  • 54,637