2

I succeded in renaming my equation names from the german word "Gleichung" into "Formel" through the following page: https://golatex.de/formelverzeichnis-erstellen-t6688,start,8.html

This equation name is used for reference through the \autoref{} function.

Now I get unwanted horizontal spaces after and before the reference like this:

enter image description here

As you can see before the "F" in the reference and after the number there is a big unwanted horizontal space.

I figured out an alternative way which I am not fully happy with:

enter image description here

Here I am using \hyperref[Pythagoras]{Formel~}\ref{Pythagoras} to refer to the equation labeled "Pythagoras". This reference excludes the space between the equation name "Formel" and the number. But I'd like the reference to be together but without the horizontal spaces. How can I achieve this?

This is my code:

\documentclass[a4paper,13pt,twoside]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage{hyperref}
\usepackage{amsmath}

\DeclareNewTOC[
indent=0pt,
hang=2em,
type=equation
]{loe}

\AtBeginDocument{
    \newcaptionname{german}\equationname{Formel}
    \newcaptionname{german}\listequationname{Formelverzeichnis}
}

\newcommand{\formelref}[1]{
    \begingroup
    \def\equationautorefname{Formel}
    \autoref{#1}
    \endgroup
}

\begin{document}
    \chapter{Pythagoras}
    \begin{equation}
    a^2 + b^2 = c^2
    \label{Pythagoras}
    \end{equation}
    \newline
    Der Satz des Pythagoras wird über die \formelref{Pythagoras} dargestellt. Möchte man nun die Länge von $c$ bestimmen, verwendet man die \formelref{cPythagoras}.
    \begin{equation}
    c = \sqrt{a^2 + b^2}
    \label{cPythagoras}
    \end{equation}
    \newline
    \textbf{Alternative:}
    \newline
    Der Satz des Pythagoras wird über die \hyperref[Pythagoras]{Formel~}\ref{Pythagoras} dargestellt. Möchte man nun die Länge von $c$ bestimmen, verwendet man die \hyperref[cPythagoras]{Formel~}\ref{cPythagoras}. % Alternative

\end{document}
Mico
  • 506,678
owmal
  • 325
  • 3
    You have three line-ends interpreted as spaces (after } and {) in your \formelref definition. Put % there to prevent them. It is complicated further because \autoref wants to look for a following space, but only sees \endgroup. – Donald Arseneau May 03 '20 at 19:26
  • That seems to work. Thank you very much! I guess this is a beginner's mistake. I did not really know why people put a % in the first place but now I can comprehend better. – owmal May 03 '20 at 19:35
  • Off-topic: You should use the option german with babel unless you have a good reason to adhere to the pre-1996 rules of German orthography. If you don't, do use the option ngerman. – Mico May 03 '20 at 20:34
  • Off-Topic: Thanks for your comment, Mico! I think, that ngerman is the new orthography ("neue Rechtschreibung") and german is the old one. So ngerman should be right. Please correct me if I am wrong, but this Question also seems to support my hypothesis: https://tex.stackexchange.com/questions/67549/whats-the-difference-between-ngerman-and-german-in-babel – owmal May 03 '20 at 20:50

2 Answers2

2

As an alternative to modifying the \autoref command and providing a new, dedicated command called \formelref, I'd like to suggest that you load the cleveref package and define "Formel" as an alias for "Gleichung". Then, use the optional argument of \label to inform cleveref when an equation should be labeled 'Formel' rather than 'Gleichung' and use \cref to cross-reference either single or multiple instances of equations. In comparison, with \autoref you can only cross-reference one object at a time.

enter image description here

\documentclass[a4paper,13pt,twoside,ngerman]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{amsmath}

\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[nameinlink]{cleveref}
\crefalias{formel}{equation}
\crefname{formel}{Formel}{Formeln}
\creflabelformat{formel}{#2\textup{(#1)}#3}

\begin{document}
\setcounter{chapter}{1}
\begin{equation} a^2+b^2=c^2 \label[formel]{Pythagoras}\end{equation}
\begin{equation} c=\sqrt{a^2+b^2} \label[formel]{cPythagoras} \end{equation}
\begin{equation} 1+1=2 \label{trivial} \end{equation} % observe: NO '[formel]'

\cref{Pythagoras}, \cref{Pythagoras,cPythagoras}; \cref{trivial}
\end{document}
Mico
  • 506,678
  • Oh, I like the Idea behind of this answer! I will test it out tomorrow. Thanks! – owmal May 03 '20 at 21:06
  • Works very good, but I get an error when labeling the equation with a german umlaut or "ß". For example, renaming \label{trivial} into \label{ß} gives me an error when using \cref{ß} but it does work with \autoref{ß}. Why is that and how can I fix this? – owmal May 04 '20 at 10:46
  • @owmal - Please advise whether you use pdfLaTeX, XeLaTeX, or LuaLaTeX. – Mico May 04 '20 at 11:08
  • I am using pdfLaTeX as my compiler. – owmal May 04 '20 at 11:11
  • @owmal - Two comments: (a) Using non-ascii characters in the argument of \label and \cref may provide a cheap thrill, but it does ask for trouble under pdflatex. I have no idea why \autoref{ß} works under pdflatex whereas \cref{ß} does not; for all I know, it's merely a case of the exception that proves the rule. (b) \cref{ß} works just fine under both XeLaTeX and LuaLaTeX, both of which process utf8-encoded characters natively. In general, if you have lots of non-ascii-encoded characters in your document, it's probably a good idea anyway to switch to either XeLaTeX or LuaLaTeX. – Mico May 04 '20 at 11:39
  • 1
    @owmal - The cleveref package must perform a lot of tricky parsing of the arguments of the user-level commands \cref and \crefrange, because multiple, comma-separated arguments may be present. (In comparison, the parsing job of \autoref is much simpler; in fact, if you try to provide multiple, comma-separated arguments to \autoref, it'll just crash.) You may have come across a real bug in the way \cref parses its argument under pdflatex. If you feel strongly about it, I would encourage you to to bring it to the attention of Toby Cubitt, the author and maintainer of cleveref. – Mico May 04 '20 at 11:44
1

As Donald pointed out, I needed to add a % after the lines and now it seems to work.

\newcommand{\formelref}[1]{%
    \begingroup%
    \def\equationautorefname{Formel}%
    \autoref{#1}%
    \endgroup%
}%

This gives me the wanted result:

enter image description here

Full Code:

\documentclass[a4paper,13pt,twoside]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage{hyperref}
\usepackage{amsmath}

\DeclareNewTOC[
indent=0pt,
hang=2em,
type=equation
]{loe}

\AtBeginDocument{
    \newcaptionname{german}\equationname{Formel}
    \newcaptionname{german}\listequationname{Formelverzeichnis}
}

\newcommand{\formelref}[1]{%
    \begingroup%
    \def\equationautorefname{Formel}%
    \autoref{#1}%
    \endgroup%
}%

\begin{document}
    \chapter{Pythagoras}
    \begin{equation}
    a^2 + b^2 = c^2
    \label{Pythagoras}
    \end{equation}
    \newline
    Der Satz des Pythagoras wird über die \formelref{Pythagoras} dargestellt. Möchte man nun die Länge von $c$ bestimmen, verwendet man die \formelref{cPythagoras}.
    \begin{equation}
    c = \sqrt{a^2 + b^2}
    \label{cPythagoras}
    \end{equation}
    \newline
    \textbf{Alternative:}
    \newline
    Der Satz des Pythagoras wird über die \hyperref[Pythagoras]{Formel~}\ref{Pythagoras} dargestellt. Möchte man nun die Länge von $c$ bestimmen, verwendet man die \hyperref[cPythagoras]{Formel~}\ref{cPythagoras}. % Alternative

\end{document}
owmal
  • 325