Define command that accepts \ in arguments












1















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}









share|improve this question

























  • So what do you expect when using your command with many lines?

    – JouleV
    1 hour ago











  • 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)

    – David Carlisle
    1 hour ago











  • Easy way to enable multiline arguments: newcommand{dfix}[2]{ensuremath{begin{array}{@{}c@{}}#1end{array}^ddag_{#2}}} but that still wouldn't make dfix{A\&B}{C} valid syntax. What do you want dfix to do?

    – Skillmon
    1 hour ago











  • 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,

    – David Carlisle
    1 hour ago











  • I just edited the question to add an example showing the desired behaviour.

    – Ryan Kavanagh
    1 hour ago
















1















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}









share|improve this question

























  • So what do you expect when using your command with many lines?

    – JouleV
    1 hour ago











  • 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)

    – David Carlisle
    1 hour ago











  • Easy way to enable multiline arguments: newcommand{dfix}[2]{ensuremath{begin{array}{@{}c@{}}#1end{array}^ddag_{#2}}} but that still wouldn't make dfix{A\&B}{C} valid syntax. What do you want dfix to do?

    – Skillmon
    1 hour ago











  • 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,

    – David Carlisle
    1 hour ago











  • I just edited the question to add an example showing the desired behaviour.

    – Ryan Kavanagh
    1 hour ago














1












1








1








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}









share|improve this question
















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}






macros errors align






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









David Carlisle

497k4111441890




497k4111441890










asked 1 hour ago









Ryan KavanaghRyan Kavanagh

404




404













  • So what do you expect when using your command with many lines?

    – JouleV
    1 hour ago











  • 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)

    – David Carlisle
    1 hour ago











  • Easy way to enable multiline arguments: newcommand{dfix}[2]{ensuremath{begin{array}{@{}c@{}}#1end{array}^ddag_{#2}}} but that still wouldn't make dfix{A\&B}{C} valid syntax. What do you want dfix to do?

    – Skillmon
    1 hour ago











  • 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,

    – David Carlisle
    1 hour ago











  • I just edited the question to add an example showing the desired behaviour.

    – Ryan Kavanagh
    1 hour ago



















  • So what do you expect when using your command with many lines?

    – JouleV
    1 hour ago











  • 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)

    – David Carlisle
    1 hour ago











  • Easy way to enable multiline arguments: newcommand{dfix}[2]{ensuremath{begin{array}{@{}c@{}}#1end{array}^ddag_{#2}}} but that still wouldn't make dfix{A\&B}{C} valid syntax. What do you want dfix to do?

    – Skillmon
    1 hour ago











  • 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,

    – David Carlisle
    1 hour ago











  • I just edited the question to add an example showing the desired behaviour.

    – Ryan Kavanagh
    1 hour ago

















So what do you expect when using your command with many lines?

– JouleV
1 hour ago





So what do you expect when using your command with many lines?

– JouleV
1 hour ago













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)

– David Carlisle
1 hour ago





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)

– David Carlisle
1 hour ago













Easy way to enable multiline arguments: newcommand{dfix}[2]{ensuremath{begin{array}{@{}c@{}}#1end{array}^ddag_{#2}}} but that still wouldn't make dfix{A\&B}{C} valid syntax. What do you want dfix to do?

– Skillmon
1 hour ago





Easy way to enable multiline arguments: newcommand{dfix}[2]{ensuremath{begin{array}{@{}c@{}}#1end{array}^ddag_{#2}}} but that still wouldn't make dfix{A\&B}{C} valid syntax. What do you want dfix to do?

– Skillmon
1 hour ago













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,

– David Carlisle
1 hour ago





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,

– David Carlisle
1 hour ago













I just edited the question to add an example showing the desired behaviour.

– Ryan Kavanagh
1 hour ago





I just edited the question to add an example showing the desired behaviour.

– Ryan Kavanagh
1 hour ago










1 Answer
1






active

oldest

votes


















3














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}





share|improve this answer
























  • 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.

    – Ryan Kavanagh
    1 hour ago











  • @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:-)

    – David Carlisle
    1 hour ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f482453%2fdefine-command-that-accepts-in-arguments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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}





share|improve this answer
























  • 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.

    – Ryan Kavanagh
    1 hour ago











  • @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:-)

    – David Carlisle
    1 hour ago
















3














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}





share|improve this answer
























  • 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.

    – Ryan Kavanagh
    1 hour ago











  • @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:-)

    – David Carlisle
    1 hour ago














3












3








3







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}





share|improve this answer













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}






share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









David CarlisleDavid Carlisle

497k4111441890




497k4111441890













  • 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.

    – Ryan Kavanagh
    1 hour ago











  • @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:-)

    – David Carlisle
    1 hour ago



















  • 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.

    – Ryan Kavanagh
    1 hour ago











  • @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:-)

    – David Carlisle
    1 hour ago

















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.

– Ryan Kavanagh
1 hour ago





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.

– Ryan Kavanagh
1 hour ago













@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:-)

– David Carlisle
1 hour ago





@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:-)

– David Carlisle
1 hour ago


















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f482453%2fdefine-command-that-accepts-in-arguments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin