2

I am trying to define a new command \dfix that can handle newlines in its arguments. The command works as expected when the arguments have no \\ in them, but LaTeX produces errors if I do include newline characters.

How can I create a command/macro that is safe with regard to line breaks?

I would like \dfix in the following context

\begin{align*}
  &\dfix{(A\\
  &B)}{C}
\end{align*

to produce the same output as

\begin{align*}
  &(A\\
  &B)^\ddag_{C}
\end{align*}

Similarly, $\dfix{A}{B}$ should produce the same output as $A^\ddag_B$.

Here is a MWE defining the command and trying to us it in various places.

\documentclass{minimal}
\usepackage{amsmath}
\newcommand{\dfix}[2]{\ensuremath{{#1}^\ddag_{#2}}}
\begin{document}
% This causes: ./mwe.tex:9: Missing } inserted.
\begin{align*}
  &\dfix{A\\
  &B}{C}
\end{align*}
% This is OK
\begin{align*}
  &\dfix{A B}{C}
\end{align*}
% This also produces errors.
\begin{multline*}
  \dfix{A\\
    B}{C}
\end{multline*}
\end{document}
6
  • So what do you expect when using your command with many lines?
    – user156344
    Mar 31, 2019 at 15:41
  • you don't say what you expect the output to be. in that context the \\ is ending the row of the align, but it is inside a group (it is the group that causes the error not the newcommand) Mar 31, 2019 at 15:41
  • Easy way to enable multiline arguments: \newcommand{\dfix}[2]{\ensuremath{\begin{array}{@{}c@{}}#1\end{array}^\ddag_{#2}}} but that still wouldn't make \dfix{A\\&B}{C} valid syntax. What do you want \dfix to do?
    – Skillmon
    Mar 31, 2019 at 15:44
  • the macro has to expand to something that is valid in the align, so you can show the intended result by showing a working align that does not use \dfix but makes the result you want. I can not guess from the code above, Mar 31, 2019 at 15:44
  • I just edited the question to add an example showing the desired behaviour. Mar 31, 2019 at 15:47

1 Answer 1

4

This does what you ask but seems a very weird construct, are you sure this is what you need?

\documentclass{minimal}
\usepackage{amsmath}
\newcommand{\dfix}[2]{(#1)^\ddag_{#2}}
\begin{document}
% This causes: ./mwe.tex:9: Missing } inserted.
\begin{align*}
  &\dfix{A\\
  &B}{C}
\end{align*}
% This is OK
\begin{align*}
  &\dfix{A B}{C}
\end{align*}
% This also produces errors.
\begin{multline*}
  \dfix{A\\
    B}{C}
\end{multline*}
\end{document}
2
  • Thanks David! The parentheses are not needed inside the body of \dfix. The intention is that $\dfix{f}{X}$ describes an operation on a functions f. When the first argument is a single letter, then no parentheses are needed (i.e., the output should be $f^\ddag_X$), but I was encountering issues when I had to split long function expression across several lines, e.g., $\dfix{(A\\B)}{X}$. Getting rid of the braces around #1 as you did and changing the definition to \newcommand{\dfix}[2]{#1^\ddag_{#2}} seems to have fixed my problem. Mar 31, 2019 at 16:01
  • @RyanKavanagh OK I would never use that markup personally, having a macro argument start in one alignment cell and end in another is extremely weird, but as I show it's not technically invalid so if it does what you want, that's OK:-) Mar 31, 2019 at 16:10

You must log in to answer this question.

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