Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

\accentset makes your command fragile so you need \protect\vec{n} or use \DeclareRobustCommand instead of \newcommand.

share|improve this answer
    
It seems, then, that I did not choose a suitable method. Which way should you gave yourself. I drew from the library physymb at page 10, where is: \ifthenelse{\boolean{pboldvectors}}% {\renewcommand{\vec}[1]{\mathbf{#1}}}% {\renewcommand{\vec}[1]{\accentset{\rightharpoonup}{#1}}} –  jafan Sep 23 '12 at 18:03
1  
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
    
Firt method (using protect) works me, but e-tex mechanism no. I tried a simple replacement \renewcommand{\vec}[1]{\accentset{\rightarrow}{#1}} by \protected\def\vec#1{\accentset{\rightarrow}{#1}. I would also like to additionally thank you for your advice. –  jafan Sep 23 '12 at 19:47
    
oh I thought that would have worked, I may test it later and add another comment –  David Carlisle Sep 23 '12 at 20:30
    
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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