0

I have created my own command with \newcommand and it Works fine with "static" input in the arguments. But, when I give e.g. a \nameref{sec:title} it is throwing errors like these:

  • Undefined control sequence
  • Missing endcsname inserted
  • Extra endcsname

How can I create a command, that supports this?

I defined the new command like this:

\newcommand{\faqFarm}[6]{
    \begin{table}[h!]
        \label{faqFarm#1}

        \begin{tabularx}{\textwidth}{@{}XXXX@{}}
             #1 & #2 & #4 & #6 \\
             & #3 & #5 & 
        \end{tabularx}
    \end{table}
}

Working example of the error:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage{hyperref}

\newcommand{\faqFarm}[6]{
    \begin{table}[htb]
        \label{faqFarm#1}

        \begin{tabularx}{\textwidth}{@{}XXXX@{}}
            \textbf{Navn:} & \textbf{Adresse:} & \textbf{Koordinater:} & \textbf{Jord:}\\
             #1 & #2 & #4 & #6 hektar\\
             & #3 & #5 & 
        \end{tabularx}
    \end{table}
}

\begin{document}
    \subsection{Working with nameref in custom command}
    \label{sec:mySubSection}

    the title one more time: \nameref{sec:mySubSection}

    \faqFarm{\nameref{sec:mySubSection}}{Bjerndrupvej 41}{6240 Løgumkloster}{Nord: 55.099754}{Øst: 9.012645}{77,625}
\end{document}
  • 1
    I do not see any \nameref in the code snippet. Also, please, post the full error message (e.g. from the .log file), because TeX does tell the name of the undefined control sequence in the error message. – Heiko Oberdiek Sep 10 '15 at 19:28
  • 1
    Please make a full compilable example that shows the intended usage of the macro and also exhibiting the issue. – egreg Sep 10 '15 at 19:42
  • @egreg I have added a full working minimum example to the question now :) – 7heViking Sep 11 '15 at 15:20
  • 1
    As David says, \label{faqFarm\nameref{sec:mySubSection}} doesn't make much sense (and breaks); but, without a caption, what's the label for? – egreg Sep 11 '15 at 15:32
2
\newcommand{\faqFarm}[6]
    \begin{table}[h!]
        \label{faqFarm#1}

        \begin{tabularx}{\textwidth}{@{}XXXX@{}}
             #1 & #2 & #4 & #6 \\
             & #3 & #5 & 
        \end{tabularx}
    \end{table}
}

You are missing { after [6]

\label should always come after (or in) \caption so will not work here

[h!] is a bad choice of option and will usually generate a warning that it has been changed. Better to use [htp] (it make no sense to routinely use !, that is just for special cases where the document constraints need to be ignored)


As noted above the \label has to come after \caption if you need to reference anything other than the page number, but if you want to do this \label{faqFarm#1} You need a new (7th) argument that forms the label text, you can not put a reference in a \label references involve tests and typesetting commands, and a label has to be an internal identifier of simple ascii characters.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage{hyperref}

\newcommand{\faqFarm}[7]{
    \begin{table}[htb]
        \label{faqFarm#7}

        \begin{tabularx}{\textwidth}{@{}XXXX@{}}
            \textbf{Navn:} & \textbf{Adresse:} & \textbf{Koordinater:} & \textbf{Jord:}\\
             #1 & #2 & #4 & #6 hektar\\
             & #3 & #5 & 
        \end{tabularx}
    \end{table}
}

\begin{document}
    \subsection{Working with nameref in custom command}
    \label{sec:mySubSection}

    the title one more time: \nameref{sec:mySubSection}

    \faqFarm{Bjerndrupvej 41}{6240 Løgumkloster}{Nord: 55.099754}{Øst: 9.012645}{77,625}{???}{sec:mySubSection}
\end{document}
  • Sorry, that was actually a copy pase error. I will prepare a fully working example right now, because it still isn't working. – 7heViking Sep 11 '15 at 14:51
  • A working example can be found here on ShareLatex.com: da.sharelatex.com/project/55f2ea97added9765e2e0c1d – 7heViking Sep 11 '15 at 14:59
  • @FireFly3000 Please fix the example on this site not external links otherwise the archival use of this site is lost. – David Carlisle Sep 11 '15 at 15:05
  • Sorry for that. I have added the minimum code to the question now. – 7heViking Sep 11 '15 at 15:18
  • @FireFly3000 answer extended to cover the case shown – David Carlisle Sep 11 '15 at 15:25

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.