3

I am trying to define a new command for different types of homomorphisms. The command is:

\newcommand{\Hom}[2][]{%
    \operatorname{Hom}_{\textnormal{#1}}\left({#2}\right)%
    }

The outputs of this command in inline and display mode, when no optional argument is given, are correct. For example this is the output for $\Hom{A,B}$ and $$\Hom{A,B}$$

Hom(A,B)

Then there could be an additional subscript to specify which type of homomorphisms I'm referring to. For example, $\Hom[gr]{G,H}$ or $$\Hom[gr]{G,H}$$ both have a "correct" output (I have still a problem with spacing between operator and first parethesis)

Hom_gr(G,H)

Finally, if I have to include some inline math in the subscript, LaTex gives me contrary error tags in displaymode. For instance, if I try to compile $\Hom[$k$-alg]{A,B}$, no errors in sight, but the same command in display mode, $$\Hom[$k$-alg]{A,B}$$, give both error and no-error. Let me explain: I receive the output (and some could argue why I'm here wainig) but at the same time there's an error allert, as in the next pictures, but no error message in the "Logs and output files".

Output

Hom_k-alg(A,B)

Errors:

"unexpected $ after $$"

"unclosed $$ found at $"

errors next to line

Is there a solution to this problem, or I just accept the error tag, and ignore it? If there's somthing I can do, I'm willing to try every advice.

Note 1: I am aware of the debate about using $$...$$ and \[...\], but I'm willing to use this new command in some already written LaTeX documents in which I've used $...$ and $$...$$, and I'd like to mantain consistency.

Note 2: Sorry for the pictures dimensions, I'm new in this of asking a question and I don't know all settings.

6
  • 3
    Welcome to TeX.SE.
    – Mico
    Commented Jan 21 at 1:00
  • Regarding the side issue, viz., "I have still a problem with spacing between operator and first parethesis": That's because the code uses \left (and \right) even when it's absolutely not needed and, in fact, can be rather counterproductive.
    – Mico
    Commented Jan 21 at 1:07
  • 1
    I receive neither error alerts nor warning messages when I run $$\Hom[$k$-alg]{A,B}$$ or, better yet, \[ \Hom[$k$-alg]{A,B} \]. I run MacTeX2023 with TeXworks as the editor; which system do you employ?
    – Mico
    Commented Jan 21 at 1:14
  • 3
    Is that the Overleaf IDE? The left side tries to analyze your source code, but can get confused when you do weird things like embed $...$ within $$...$$. What really matters is if you have a red number above your pdf.
    – Teepeemm
    Commented Jan 21 at 1:28
  • @mico I'm using the overleaf compiler, and as stated by @teepeemm, it's just a problem with the inside working of this compiler. I was worried for nothing. Regarding the parenthesis, my example was not oriented to show that, I might open another question explaining why I was using \left( and \right). Commented Mar 14 at 19:09

1 Answer 1

5

These are not errors, but just wrong advice from Overleaf that is sometimes unable to comply with nested math formulas.

It probably wouldn't balk if you write explicitly [\textnormal{$k$-alg}] as the optional argument, but isn't able to guess that the arguments is passed to \textnormal.

So, ignore those messages.

There are other important things about your code:

  1. \left and \right should be omitted;
  2. the subscript will produce an unwanted space when missing;
  3. $$ should never be used in a LaTeX document environment.
\documentclass[twocolumn]{article}
\usepackage{amsmath}

\NewDocumentCommand{\Hom}{o}{%
  \operatorname{Hom}\IfValueT{#1}{_{\textnormal{#1}}}%
}

\newcommand{\lorenzoHom}[2][]{%
    \operatorname{Hom}_{\textnormal{#1}}\left({#2}\right)%
    }

\begin{document}

\subsubsection*{My definition}

\begin{gather*}
\Hom(A,B) \\
\Hom[$k$-alg](A,B)
\end{gather*}

\subsubsection*{Your definition}

\begin{gather*}
\lorenzoHom{A,B} \\
\lorenzoHom[$k$-alg]{A,B}
\end{gather*}

\end{document}

I don't think there's a gain in using braces {} around A,B just in order to get parentheses: it's more natural to input (A,B) directly.

enter image description here

Can you see the unwanted spaces?

1
  • thank you for your answer! I have explained why I was using \left( and \right) in this answer, to be more clear, if you'd like to have a look. Commented Mar 17 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .