1

I am trying to define a snippet which substitutes text in multiple places. As a trivial example see the following which works for me :

snippet abc "Testing" bmA
\begin{${1:text_here}}
 $0
\end{${1:text_here}}

Now, when this snippet is expanded whatever I substitute for text_here in the first instance gets automatically substituted in the second instance as well. I want to expand this to write the preamble of my code as follows :

snippet notes "creates a template to take notes" bm

% !TEX program = xelatex

\documentclass[${1:12pt}]{${2:article}}

\usepackage[fleqn]{amsmath}
\usepackage{amssymb}
\usepackage{bm} %for boldface math

\usepackage{braket}
\usepackage[colorlinks,citecolor=red,urlcolor=blue,bookmarks=false,hypertexnames=true]{hyperref}

${3:%}\usepackage[math-style=TeX, bold-style=TeX]{unicode-math}
${3:%}\setmainfont{EB Garamond}
${3:%}\setmathfont{Garamond-Math.otf}[StylisticSet={6,10}]

${4:%}\usepackage[T1]{fontenc}
${4:%}\usepackage{garamondx}
${4:%}\usepackage[garamondx,cmbraces]{newtxmath}
${4:%}\usepackage{bm} %for boldface math
%\usepackage{anyfontsize}

\newcommand{\diff}{\mathop{}\!\mathrm{d}}

\usepackage[a4paper, scale=0.9]{geometry}
\usepackage{tcolorbox}

\title{${5:Title}}
\author{${6:Author}}
${7:%}\date{}

\begin{document}
\begin{tcolorbox}
 \maketitle
\end{tcolorbox}
%\par\noindent\rule[0.5cm]{\textwidth}{1pt}

${0:start_hammering}

\newpage
\begin{thebibliography}{99}



\end{thebibliography}
\end{document}
endsnippet

This fails to work. What I want to do is achieve similar result as that of the simple example I showed above. When my cursor reaches $3 and I start typing : it is only substituted in one instance of $3 and not all the instances. Likewise for $4 as well. To be crystal clear, my goal here is to do the following : if I delete the default text (%) appearing at the first instance of $3 and all the instances of $3 should be modified accordingly.

Can someone please help me figure out how to correct this?

1

You should only set a default value on the first occurrence of the placeholder, that way it works.

From :help UltiSnips-mirrors:

[...] A mirrored tabstop can have a default value defined. Only the first instance of the tabstop need have a default value. Mirrored tabstop will take on the default value automatically.

While the help says "need", it only works for me (including on your abc example snippet) when the mirrors have no default value.

So where you currently have this:

${3:%}\usepackage[math-style=TeX, bold-style=TeX]{unicode-math}
${3:%}\setmainfont{EB Garamond}
${3:%}\setmathfont{Garamond-Math.otf}[StylisticSet={6,10}]

${4:%}\usepackage[T1]{fontenc}
${4:%}\usepackage{garamondx}
${4:%}\usepackage[garamondx,cmbraces]{newtxmath}
${4:%}\usepackage{bm} %for boldface math
%\usepackage{anyfontsize}

Use this instead:

${3:%}\usepackage[math-style=TeX, bold-style=TeX]{unicode-math}
$3\setmainfont{EB Garamond}
$3\setmathfont{Garamond-Math.otf}[StylisticSet={6,10}]

${4:%}\usepackage[T1]{fontenc}
$4\usepackage{garamondx}
$4\usepackage[garamondx,cmbraces]{newtxmath}
$4\usepackage{bm} %for boldface math
%\usepackage{anyfontsize}
2
  • 1
    Working for me when only the first mirror has a default value and rest have no value. Thank you. Should have read the documentation :(
    – Nitin
    May 21 '20 at 1:10
  • 2
    @Nitin To be fair, my re-reading of the docs imply that it should work when more than one has the default, it only says they don't need to have the default... Perhaps the docs could be improved here a little bit... Glad this fixed it for you though!
    – filbranden
    May 21 '20 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.