# Problem with changing refname with autoref in Lyx

Update 1 - @Andrew provided a very helpful partial answer. The \renewcommands work if you put them after begin document in the latex version. So the real question is why does the preamble not do this in the lyx version. It seems that \AtBeginDocument does not work as it should.

Update 2 - included minimal example.

Update 3 - it seems that upgrading to LyX 2.06 is the best solution to this problem. The bug in the default thesis template has been fixed by that version. Thank you to @scottkosty for pointing this out.

I am using the default thesis template in Lyx 2.02 and have a problem with cross references. In the pdf produced by Lyx I see the cross reference as "subsubsection 1.3.2.3", but I want "sec. 1.3.2.3". I believe I just change the names in the following code from the Latex preamble, but I have tried many different ways of doing this and any changes to the names are ignored in the pdf produced (see minimal example below). Any ideas on what I am doing wrong?

The preamble code is

\AtBeginDocument{%
\renewcommand{\equationautorefname}{\hspace{-\abc}}
\renewcommand{\sectionautorefname}{sec.\negthinspace}
\renewcommand{\subsectionautorefname}{sec.\negthinspace}
\renewcommand{\subsubsectionautorefname}{sec.\negthinspace}
\renewcommand{\figureautorefname}{Fig.\negthinspace}
\renewcommand{\tableautorefname}{Tab.\negthinspace}
}
}


As a minimal example, I copied the default thesis template preamble and pasted it into an empty new document. I added a single subsubsection and a reference to it. It should appear as "sec. 0.0.0.1" but appears as "subsubsection 0.0.0.1". The Lyx code is

#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\textclass scrbook
\begin_preamble
% increase link area for cross-references and autoname them
\AtBeginDocument{\renewcommand{\ref}[1]{\mbox{\autoref{#1}}}}
\newlength{\abc}
\settowidth{\abc}{\space}
\AtBeginDocument{%
\renewcommand{\equationautorefname}{\hspace{-\abc}}
\renewcommand{\sectionautorefname}{sec.\negthinspace}
\renewcommand{\subsectionautorefname}{sec.\negthinspace}
\renewcommand{\subsubsectionautorefname}{sec.\negthinspace}
\renewcommand{\figureautorefname}{Fig.\negthinspace}
\renewcommand{\tableautorefname}{Tab.\negthinspace}
}
}
\end_preamble
\use_default_options true
\maintain_unincluded_children false
\language british
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100

\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref true
\pdf_bookmarks true
\pdf_bookmarksnumbered false
\pdf_bookmarksopen false
\pdf_bookmarksopenlevel 1
\pdf_pdfborder false
\pdf_backref false
\pdf_pdfusetitle true
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation skip
\defskip smallskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false

\begin_body

\begin_layout Standard
Minimal example of autoref problem.
Here is a reference to a subsubsection -
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Test-subsubsection"

\end_inset

\end_layout

\begin_layout Subsubsection
\begin_inset CommandInset label
LatexCommand label
name "sub:Test-subsubsection"

\end_inset

Test subsubsection
\end_layout

\end_body
\end_document


The exported latex code looks like:

%% LyX 2.0.2 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[oneside,british]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\setlength{\parskip}{\smallskipamount}
\setlength{\parindent}{0pt}
\usepackage{babel}
\usepackage[unicode=true,pdfusetitle,
bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
{hyperref}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
% increase link area for cross-references and autoname them
\AtBeginDocument{\renewcommand{\ref}[1]{\mbox{\autoref{#1}}}}
\newlength{\abc}
\settowidth{\abc}{\space}
\AtBeginDocument{%
\renewcommand{\equationautorefname}{\hspace{-\abc}}
\renewcommand{\sectionautorefname}{sec.\negthinspace}
\renewcommand{\subsectionautorefname}{sec.\negthinspace}
\renewcommand{\subsubsectionautorefname}{sec.\negthinspace}
\renewcommand{\figureautorefname}{Fig.\negthinspace}
\renewcommand{\tableautorefname}{Tab.\negthinspace}
}
}

\makeatother

\begin{document}
Minimal example of autoref problem. Here is a reference to a subsubsection
- \ref{sub:Test-subsubsection}

\subsubsection{\label{sub:Test-subsubsection}Test subsubsection}
\end{document}

• Can you please provide complete minimal example files for both .tex and .lyx. See the LyX Wiki for info on a LyX minimal example file May 24 '13 at 7:23
• See the updates to the question. I hope this is what you wanted.
– motz
May 24 '13 at 9:10

Having two AtBeginDocument commands seems unnecessary (and maybe problematic). Having the following in LyX's LaTeX preamble worked for me:

\newlength{\abc}
\settowidth{\abc}{\space}
\AtBeginDocument{%
\renewcommand{\ref}[1]{\mbox{\autoref{#1}}}
\renewcommand{\equationautorefname}{\hspace{-\abc}}
\renewcommand{\sectionautorefname}{sec.\negthinspace}
\renewcommand{\subsectionautorefname}{sec.\negthinspace}
\renewcommand{\subsubsectionautorefname}{sec.\negthinspace}
\renewcommand{\figureautorefname}{Fig.\negthinspace}
\renewcommand{\tableautorefname}{Tab.\negthinspace}
}

• This fixes the problem in the minimal example I provided. It means there is bug in the default thesis template of Lyx. Unfortunately it does not fix the problem in the actual thesis - something must have been modified from the default :-( . But I found a workaround using @Andrew's approach by putting it in evil red text at the start of the master document and each sub-document. Thank you both for solving the problem.
– motz
May 25 '13 at 7:20
• Glad you worked it out. I think this might have been fixed in the newer versions of LyX. I just checked and the preambles are different than the one you started out with. If you're using Ubuntu by chance, there's an easy way to install the newer version alongside your current version so that you don't have to worry about going back in case something goes wrong. May 25 '13 at 7:39
• Thank you for pointing that out. I upgraded to Lyx 2.06 and it does have a modified preamble for the default thesis template and that solves the problem.
– motz
May 26 '13 at 8:11
• Thank you this worked for me on the Lyx 2.1.4 dev branch. In contrast to the Hyperref doc, the trick was to remove the Babel related \addto\extrasenglish{}. Also to make sure there was only one \AtBeginDocument{} in the preamble.
– ʀᴏʙ
Mar 13 '15 at 20:56

What you have works fine for me although I use \autoref instead of \ref and I am just using the article class as I don't have LyX installed.

I suspect that your problem may be that lyx is over writing your new definitions of \sectionautorefname etc. I suggest putting your renewcommands after the \begin{document} and see if this fixes the problem.

\begin{document}
\renewcommand{\equationautorefname}{\hspace{-\abc}
\renewcommand{\sectionautorefname}{fred.\negthinspace}
\renewcommand{\subsectionautorefname}{sec.\negthinspace}
\renewcommand{\subsubsectionautorefname}{sec.\negthinspace}
\renewcommand{\figureautorefname}{Fig.\negthinspace}
\renewcommand{\tableautorefname}{Tab.\negthinspace} }

\section{1 section}\label{sec1}
\section{2 section}\label{sec2}

See \autoref{sec1} and \autoref{sec2}.
\end{document}

• This does fix the problem which is very helpful thank you, but it does depend on being able to export latex from lyx and then modify the latex. The export process is difficult for a large document like a thesis. Do you have any idea of how to do this within Lyx?
– motz
May 24 '13 at 8:30