# Add custom code snippets with AUCTeX

I have started using Emacs a couple weeks ago and so far I got it to do pretty much everything I wanted. Recently, I started using Emacs and AUCTeX to write a GUI specification document. For each GUI element I have a corresponding LaTeX snippet that I can insert in the specification to start from, like this, for example:

\item \texttt{Button}
\begin{description}
\item[Label]    Here I add a description for the wanted button label...
\item[Enabled]
\item[Visible]
\item[Tooltip]
\item[Alternate control]
\item[Events]
\item[Misc]
\end{description}

To include it in the specification, I cut and paste it from another document. Copying and pasting works, but is tedious. What I would like to achieve with Emacs and AUCTeX is to type, from within Emacs, something like M-x button so that it adds the corresponding code snippet directly from the current cursor position. I'd like this snippet to be available only when a .tex file is opened.

How can I do this?

Check out YASnippet Emacs package. M-x package-install RET yasnippet. Might find this useful or just Google for examples: tutorial Once installed and you have (yas-global-mode t) in your .emacs file, it will show a menu bar YASnippet.

Once YASnippet is installed try this. Save the following in a file ~/.emacs.d/snippets/latex-mode/test.yasnippet

# -*- mode: snippet -*-
# name: test
# key: test_
# --
\item \texttt{Button}
\begin{description}
\item[Label]     ${1:Add description} \item[Readonly] etc... \item[Enabled] \item[Visible] \item[Tooltip] \item[Alternate control] \item[Events] \item[Misc] \end{description}$0

Then when you type "test_" TAB it should insert your code at point. Once you type in your description press TAB it will take you to the end of your code. The $1,$2, etc is the location of the cursor after each TAB, \$0 is last TAB.

• The tutorial looks promising! – BobMorane Jan 3 '18 at 10:19
• This works very well. – BobMorane Jan 3 '18 at 10:32
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.30568817257881165, "perplexity": 3218.742452242097}, "config": {"markdown_headings": true, "markdown_code": false, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 5, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-21/segments/1620243989812.47/warc/CC-MAIN-20210515035645-20210515065645-00190.warc.gz"}