Using (redefined) macro in \caption doesn't work, but it works in another environment

My problem is strange behavior my command \vec{} in \caption. I used this construction:

\renewcommand{\vec}[1]{\accentset{\rightarrow}{#1}}


See my code:

\documentclass[12pt, a4paper, czech, twoside]{book}
\usepackage{amsmath, amsthm, amssymb, amsfonts, amsbsy}
\usepackage{accents}
\usepackage{fontspec,xltxtra,xunicode,unicode-math}
\setmainfont[Mapping=tex-text,Numbers={OldStyle,Proportional}]{Calibri}
\setsansfont[Mapping=tex-text,Numbers={OldStyle,Proportional}]{Candara}
\setmonofont[Numbers=OldStyle]{Consolas}
\setmathfont{Cambria Math}
\usepackage{tikz}

\begin{document}

\renewcommand{\vec}[1]{\accentset{\rightarrow}{#1}}

\begin{figure}[hb!]
\centering
\begin{tikzpicture}
\coordinate (dS) at (2,0.5);
\draw[->] (dS) ++ (0.25,0.25) -- +(1,0.5) node[right]{$\vec{n}$};
\draw[->] (dS) ++ (0.25,0.25) -- +(2,1.5) node[right]{$\vec{h}$};
\end{tikzpicture}
\label{FYZ:fig_01}
\caption{vector $\vec{n}$.}
\end{figure}

\end{document}


SW: MiKTeX 2.9 64 bit up to date. When I try to use the command \vec in a tikzpicture environment, everything all right, but with caption an error occurs. I don't understand it. What am I doing wrong?

-
\accentset makes your command fragile so you need \protect\vec{n} or use \DeclareRobustCommand instead of \newcommand.
needing \protect did not mean you did anything wrong (many standard latex commands need \protect) however as the memory available now is so much more than available in 1990's it makes sense to use a bit extra memory and make your definition robust. Using \DeclareRobustCommand is the easiest to use as teh syntax is like \newcommand. An alternative would be to use the e-tex mechanism \protected\def\vec#1{\accentset{\rightarrow}{#1} e-tex protection is technically different from the LaTeX \protect mechanism (which pre-dated e-tex) but in cases like this you can use either –  David Carlisle Sep 23 '12 at 18:09
@jafan,david \protected \def\vec#1{\accentset{\rightarrow}{#1} is missing a final closing brace: is this the error we're talking about, or is it something else? –  wasteofspace Sep 24 '12 at 11:00