Macro that takes any latex code as an argument












7















So I am thinking of creating a macro that takes any latex code as an argument. For example,



newcommandpurp[1]{color{purple} #1}


I hoped that this macro would make any latex code inside of purp to be colored as purple. I realized, however, is that if I pass some environment (begin.. end) to the argument, some weird latex error occurs. For example.



purp{
begin{verbatim}
asdfadf
asdf
asdf
end{verbatim}
}


breaks the verbatim output and generates bunch of latex errors. Is there any safe way to make a function that takes arbitrary latex code as an argument?



Edit



Besides a verbatim, I also observed that following produces very weird error.



purp{
[
x= begin{cases*}
y & if x_1 text{ is } 1\
z & if x_2 text{ is } 2
end{cases*}
]
}


This gives a weird error Missing $ is inserted. Any idea why?










share|improve this question




















  • 5





    The problem is not the environment, but the verbatim. Verbatim can not be in the argument of other commands.

    – Ulrike Fischer
    May 8 at 9:56
















7















So I am thinking of creating a macro that takes any latex code as an argument. For example,



newcommandpurp[1]{color{purple} #1}


I hoped that this macro would make any latex code inside of purp to be colored as purple. I realized, however, is that if I pass some environment (begin.. end) to the argument, some weird latex error occurs. For example.



purp{
begin{verbatim}
asdfadf
asdf
asdf
end{verbatim}
}


breaks the verbatim output and generates bunch of latex errors. Is there any safe way to make a function that takes arbitrary latex code as an argument?



Edit



Besides a verbatim, I also observed that following produces very weird error.



purp{
[
x= begin{cases*}
y & if x_1 text{ is } 1\
z & if x_2 text{ is } 2
end{cases*}
]
}


This gives a weird error Missing $ is inserted. Any idea why?










share|improve this question




















  • 5





    The problem is not the environment, but the verbatim. Verbatim can not be in the argument of other commands.

    – Ulrike Fischer
    May 8 at 9:56














7












7








7








So I am thinking of creating a macro that takes any latex code as an argument. For example,



newcommandpurp[1]{color{purple} #1}


I hoped that this macro would make any latex code inside of purp to be colored as purple. I realized, however, is that if I pass some environment (begin.. end) to the argument, some weird latex error occurs. For example.



purp{
begin{verbatim}
asdfadf
asdf
asdf
end{verbatim}
}


breaks the verbatim output and generates bunch of latex errors. Is there any safe way to make a function that takes arbitrary latex code as an argument?



Edit



Besides a verbatim, I also observed that following produces very weird error.



purp{
[
x= begin{cases*}
y & if x_1 text{ is } 1\
z & if x_2 text{ is } 2
end{cases*}
]
}


This gives a weird error Missing $ is inserted. Any idea why?










share|improve this question
















So I am thinking of creating a macro that takes any latex code as an argument. For example,



newcommandpurp[1]{color{purple} #1}


I hoped that this macro would make any latex code inside of purp to be colored as purple. I realized, however, is that if I pass some environment (begin.. end) to the argument, some weird latex error occurs. For example.



purp{
begin{verbatim}
asdfadf
asdf
asdf
end{verbatim}
}


breaks the verbatim output and generates bunch of latex errors. Is there any safe way to make a function that takes arbitrary latex code as an argument?



Edit



Besides a verbatim, I also observed that following produces very weird error.



purp{
[
x= begin{cases*}
y & if x_1 text{ is } 1\
z & if x_2 text{ is } 2
end{cases*}
]
}


This gives a weird error Missing $ is inserted. Any idea why?







macros arguments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 9 at 1:44







Jaebum

















asked May 8 at 9:48









JaebumJaebum

1455




1455








  • 5





    The problem is not the environment, but the verbatim. Verbatim can not be in the argument of other commands.

    – Ulrike Fischer
    May 8 at 9:56














  • 5





    The problem is not the environment, but the verbatim. Verbatim can not be in the argument of other commands.

    – Ulrike Fischer
    May 8 at 9:56








5




5





The problem is not the environment, but the verbatim. Verbatim can not be in the argument of other commands.

– Ulrike Fischer
May 8 at 9:56





The problem is not the environment, but the verbatim. Verbatim can not be in the argument of other commands.

– Ulrike Fischer
May 8 at 9:56










3 Answers
3






active

oldest

votes


















8














The near universal rule is that verbatim material cannot be an argument to a command. With the cprotect package, there is sometimes a workaround as shown in the MWE, but even that is not a universal fix.



EDIT: group added to prevent spillage of purple into subsequent material (thanks Ulrike).



documentclass{article}
usepackage{cprotect,xcolor}
newcommandpurp[1]{{color{purple} #1}}
begin{document}
cprotectpurp{
begin{verbatim}
asdfadf
asdf
asdf
end{verbatim}
}
end{document}


enter image description here






share|improve this answer





















  • 4





    you should add a group to avoid that following text is purple too.

    – Ulrike Fischer
    May 8 at 10:05











  • @UlrikeFischer Thank you for reminding me of that.

    – Steven B. Segletes
    May 8 at 10:08











  • awesome thank you!

    – Jaebum
    May 9 at 1:36











  • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

    – Jaebum
    May 9 at 1:45






  • 2





    @Jaebum Your syntax is in error: purp{ [ x= begin{cases*} y & if $x_1$ is 1\ z & if $x_2$ is 2 end{cases*} ] } Nothing at all to do with purp.

    – Steven B. Segletes
    May 9 at 1:51





















5














If it's just about colours, you could fake a textcolor command, which seems to take an argument, but does support verbatim material. Note that this is no general solution, but just provides an argument taking syntax to color which is not really an argument.



(This way one could use any formatting switches not only colours, e.g. bfseries could be used this way, too)



EDIT: created a switchhack that allows arbitrary formatting switches not only colours, and colorhack calls switchhack now.



documentclass{article}

usepackage{xcolor}

makeatletter
begingroupdef:#1{endgroupletswitchhack@sptoken= #1}:{ }
newcommandswitchhack[1]
{%
defswitchhack@switch{#1}%
switchhack@a
}
newcommandswitchhack@a
{%
futureletswitchhack@gobbledswitchhack@b
}
newcommandswitchhack@b
{%
ifxswitchhack@gobbledswitchhack@sptoken
switchhack@eatspace
fi
@firstofone
{%
ifxbgroupswitchhack@gobbled
else
GenericWarning
{}{Warning: Argument of stringswitchhackspace not delimited}%
switchhack@undelimeted
fi
switchhack@delimited
}%
}
longdefswitchhack@eatspacefi#1#2%
{%
fi
afterassignmentswitchhack@a
letswitchhack@gobbled=
}
longdefswitchhack@undelimetedfi#1#2%
{%
fi
{switchhack@switch#2}%
}
newcommandswitchhack@delimited
{%
afterassignmentswitchhack@do
letswitchhack@gobbled
}
newcommandswitchhack@do
{%
bgroupswitchhack@switch
}
longdefcolorhack#1#{colorhack@{#1}}
newcommandcolorhack@[2]
{%
switchhack{color#1{#2}}%
}
makeatother

newcommandpurp{colorhack{purple}}
newcommandbfhack{switchhack{bfseries}}

begin{document}
colorhack[rgb]{.5 .4 .8}{foo}
purp{verb|this is blue|}
following text
bfhack{this is bold}

colorhack{red} Undelimeted

purp{
begin{verbatim}
This is
verbatim
purple (I lied up there, it never was blue)
end{verbatim}
}
end{document}


enter image description here






share|improve this answer


























  • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

    – Jaebum
    May 9 at 1:45



















2














Another possibility to get macros which take verbatim material as arguments is to grab the arguments as boxes instead. Again this is no universal solution and you can't do arbitrary stuff with it (most importantly you can't change the appearance of the boxed material after it is boxed, that's what the optional arguments of grabbox are for, to make some formatting possible).



The following defines purp to take its argument inside of a hbox and purp* to take it inside of a vbox (of course the macro name in both cases is purp, the second one is just followed by a star).



documentclass{article}

usepackage{grabbox}
usepackage{xcolor}

makeatletter
newsaveboxpurp@box
newcommandpurp
{%
begingroup
@ifstar
{%
grabboxpurp@box[color{purple}]vbox{unvboxpurp@boxendgroup}%
}
{%
grabboxpurp@box[color{purple}]hbox{unhboxpurp@boxendgroup}%
}%
}
makeatother

begin{document}
purp{verb|this is blue|}

other text

purp*{
begin{verbatim}
This is
verbatim
purple (I lied up there, it never was blue)
end{verbatim}
}
end{document}


enter image description here






share|improve this answer
























    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%2f489767%2fmacro-that-takes-any-latex-code-as-an-argument%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    8














    The near universal rule is that verbatim material cannot be an argument to a command. With the cprotect package, there is sometimes a workaround as shown in the MWE, but even that is not a universal fix.



    EDIT: group added to prevent spillage of purple into subsequent material (thanks Ulrike).



    documentclass{article}
    usepackage{cprotect,xcolor}
    newcommandpurp[1]{{color{purple} #1}}
    begin{document}
    cprotectpurp{
    begin{verbatim}
    asdfadf
    asdf
    asdf
    end{verbatim}
    }
    end{document}


    enter image description here






    share|improve this answer





















    • 4





      you should add a group to avoid that following text is purple too.

      – Ulrike Fischer
      May 8 at 10:05











    • @UlrikeFischer Thank you for reminding me of that.

      – Steven B. Segletes
      May 8 at 10:08











    • awesome thank you!

      – Jaebum
      May 9 at 1:36











    • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

      – Jaebum
      May 9 at 1:45






    • 2





      @Jaebum Your syntax is in error: purp{ [ x= begin{cases*} y & if $x_1$ is 1\ z & if $x_2$ is 2 end{cases*} ] } Nothing at all to do with purp.

      – Steven B. Segletes
      May 9 at 1:51


















    8














    The near universal rule is that verbatim material cannot be an argument to a command. With the cprotect package, there is sometimes a workaround as shown in the MWE, but even that is not a universal fix.



    EDIT: group added to prevent spillage of purple into subsequent material (thanks Ulrike).



    documentclass{article}
    usepackage{cprotect,xcolor}
    newcommandpurp[1]{{color{purple} #1}}
    begin{document}
    cprotectpurp{
    begin{verbatim}
    asdfadf
    asdf
    asdf
    end{verbatim}
    }
    end{document}


    enter image description here






    share|improve this answer





















    • 4





      you should add a group to avoid that following text is purple too.

      – Ulrike Fischer
      May 8 at 10:05











    • @UlrikeFischer Thank you for reminding me of that.

      – Steven B. Segletes
      May 8 at 10:08











    • awesome thank you!

      – Jaebum
      May 9 at 1:36











    • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

      – Jaebum
      May 9 at 1:45






    • 2





      @Jaebum Your syntax is in error: purp{ [ x= begin{cases*} y & if $x_1$ is 1\ z & if $x_2$ is 2 end{cases*} ] } Nothing at all to do with purp.

      – Steven B. Segletes
      May 9 at 1:51
















    8












    8








    8







    The near universal rule is that verbatim material cannot be an argument to a command. With the cprotect package, there is sometimes a workaround as shown in the MWE, but even that is not a universal fix.



    EDIT: group added to prevent spillage of purple into subsequent material (thanks Ulrike).



    documentclass{article}
    usepackage{cprotect,xcolor}
    newcommandpurp[1]{{color{purple} #1}}
    begin{document}
    cprotectpurp{
    begin{verbatim}
    asdfadf
    asdf
    asdf
    end{verbatim}
    }
    end{document}


    enter image description here






    share|improve this answer















    The near universal rule is that verbatim material cannot be an argument to a command. With the cprotect package, there is sometimes a workaround as shown in the MWE, but even that is not a universal fix.



    EDIT: group added to prevent spillage of purple into subsequent material (thanks Ulrike).



    documentclass{article}
    usepackage{cprotect,xcolor}
    newcommandpurp[1]{{color{purple} #1}}
    begin{document}
    cprotectpurp{
    begin{verbatim}
    asdfadf
    asdf
    asdf
    end{verbatim}
    }
    end{document}


    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 8 at 10:07

























    answered May 8 at 9:59









    Steven B. SegletesSteven B. Segletes

    166k9210427




    166k9210427








    • 4





      you should add a group to avoid that following text is purple too.

      – Ulrike Fischer
      May 8 at 10:05











    • @UlrikeFischer Thank you for reminding me of that.

      – Steven B. Segletes
      May 8 at 10:08











    • awesome thank you!

      – Jaebum
      May 9 at 1:36











    • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

      – Jaebum
      May 9 at 1:45






    • 2





      @Jaebum Your syntax is in error: purp{ [ x= begin{cases*} y & if $x_1$ is 1\ z & if $x_2$ is 2 end{cases*} ] } Nothing at all to do with purp.

      – Steven B. Segletes
      May 9 at 1:51
















    • 4





      you should add a group to avoid that following text is purple too.

      – Ulrike Fischer
      May 8 at 10:05











    • @UlrikeFischer Thank you for reminding me of that.

      – Steven B. Segletes
      May 8 at 10:08











    • awesome thank you!

      – Jaebum
      May 9 at 1:36











    • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

      – Jaebum
      May 9 at 1:45






    • 2





      @Jaebum Your syntax is in error: purp{ [ x= begin{cases*} y & if $x_1$ is 1\ z & if $x_2$ is 2 end{cases*} ] } Nothing at all to do with purp.

      – Steven B. Segletes
      May 9 at 1:51










    4




    4





    you should add a group to avoid that following text is purple too.

    – Ulrike Fischer
    May 8 at 10:05





    you should add a group to avoid that following text is purple too.

    – Ulrike Fischer
    May 8 at 10:05













    @UlrikeFischer Thank you for reminding me of that.

    – Steven B. Segletes
    May 8 at 10:08





    @UlrikeFischer Thank you for reminding me of that.

    – Steven B. Segletes
    May 8 at 10:08













    awesome thank you!

    – Jaebum
    May 9 at 1:36





    awesome thank you!

    – Jaebum
    May 9 at 1:36













    I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

    – Jaebum
    May 9 at 1:45





    I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

    – Jaebum
    May 9 at 1:45




    2




    2





    @Jaebum Your syntax is in error: purp{ [ x= begin{cases*} y & if $x_1$ is 1\ z & if $x_2$ is 2 end{cases*} ] } Nothing at all to do with purp.

    – Steven B. Segletes
    May 9 at 1:51







    @Jaebum Your syntax is in error: purp{ [ x= begin{cases*} y & if $x_1$ is 1\ z & if $x_2$ is 2 end{cases*} ] } Nothing at all to do with purp.

    – Steven B. Segletes
    May 9 at 1:51













    5














    If it's just about colours, you could fake a textcolor command, which seems to take an argument, but does support verbatim material. Note that this is no general solution, but just provides an argument taking syntax to color which is not really an argument.



    (This way one could use any formatting switches not only colours, e.g. bfseries could be used this way, too)



    EDIT: created a switchhack that allows arbitrary formatting switches not only colours, and colorhack calls switchhack now.



    documentclass{article}

    usepackage{xcolor}

    makeatletter
    begingroupdef:#1{endgroupletswitchhack@sptoken= #1}:{ }
    newcommandswitchhack[1]
    {%
    defswitchhack@switch{#1}%
    switchhack@a
    }
    newcommandswitchhack@a
    {%
    futureletswitchhack@gobbledswitchhack@b
    }
    newcommandswitchhack@b
    {%
    ifxswitchhack@gobbledswitchhack@sptoken
    switchhack@eatspace
    fi
    @firstofone
    {%
    ifxbgroupswitchhack@gobbled
    else
    GenericWarning
    {}{Warning: Argument of stringswitchhackspace not delimited}%
    switchhack@undelimeted
    fi
    switchhack@delimited
    }%
    }
    longdefswitchhack@eatspacefi#1#2%
    {%
    fi
    afterassignmentswitchhack@a
    letswitchhack@gobbled=
    }
    longdefswitchhack@undelimetedfi#1#2%
    {%
    fi
    {switchhack@switch#2}%
    }
    newcommandswitchhack@delimited
    {%
    afterassignmentswitchhack@do
    letswitchhack@gobbled
    }
    newcommandswitchhack@do
    {%
    bgroupswitchhack@switch
    }
    longdefcolorhack#1#{colorhack@{#1}}
    newcommandcolorhack@[2]
    {%
    switchhack{color#1{#2}}%
    }
    makeatother

    newcommandpurp{colorhack{purple}}
    newcommandbfhack{switchhack{bfseries}}

    begin{document}
    colorhack[rgb]{.5 .4 .8}{foo}
    purp{verb|this is blue|}
    following text
    bfhack{this is bold}

    colorhack{red} Undelimeted

    purp{
    begin{verbatim}
    This is
    verbatim
    purple (I lied up there, it never was blue)
    end{verbatim}
    }
    end{document}


    enter image description here






    share|improve this answer


























    • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

      – Jaebum
      May 9 at 1:45
















    5














    If it's just about colours, you could fake a textcolor command, which seems to take an argument, but does support verbatim material. Note that this is no general solution, but just provides an argument taking syntax to color which is not really an argument.



    (This way one could use any formatting switches not only colours, e.g. bfseries could be used this way, too)



    EDIT: created a switchhack that allows arbitrary formatting switches not only colours, and colorhack calls switchhack now.



    documentclass{article}

    usepackage{xcolor}

    makeatletter
    begingroupdef:#1{endgroupletswitchhack@sptoken= #1}:{ }
    newcommandswitchhack[1]
    {%
    defswitchhack@switch{#1}%
    switchhack@a
    }
    newcommandswitchhack@a
    {%
    futureletswitchhack@gobbledswitchhack@b
    }
    newcommandswitchhack@b
    {%
    ifxswitchhack@gobbledswitchhack@sptoken
    switchhack@eatspace
    fi
    @firstofone
    {%
    ifxbgroupswitchhack@gobbled
    else
    GenericWarning
    {}{Warning: Argument of stringswitchhackspace not delimited}%
    switchhack@undelimeted
    fi
    switchhack@delimited
    }%
    }
    longdefswitchhack@eatspacefi#1#2%
    {%
    fi
    afterassignmentswitchhack@a
    letswitchhack@gobbled=
    }
    longdefswitchhack@undelimetedfi#1#2%
    {%
    fi
    {switchhack@switch#2}%
    }
    newcommandswitchhack@delimited
    {%
    afterassignmentswitchhack@do
    letswitchhack@gobbled
    }
    newcommandswitchhack@do
    {%
    bgroupswitchhack@switch
    }
    longdefcolorhack#1#{colorhack@{#1}}
    newcommandcolorhack@[2]
    {%
    switchhack{color#1{#2}}%
    }
    makeatother

    newcommandpurp{colorhack{purple}}
    newcommandbfhack{switchhack{bfseries}}

    begin{document}
    colorhack[rgb]{.5 .4 .8}{foo}
    purp{verb|this is blue|}
    following text
    bfhack{this is bold}

    colorhack{red} Undelimeted

    purp{
    begin{verbatim}
    This is
    verbatim
    purple (I lied up there, it never was blue)
    end{verbatim}
    }
    end{document}


    enter image description here






    share|improve this answer


























    • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

      – Jaebum
      May 9 at 1:45














    5












    5








    5







    If it's just about colours, you could fake a textcolor command, which seems to take an argument, but does support verbatim material. Note that this is no general solution, but just provides an argument taking syntax to color which is not really an argument.



    (This way one could use any formatting switches not only colours, e.g. bfseries could be used this way, too)



    EDIT: created a switchhack that allows arbitrary formatting switches not only colours, and colorhack calls switchhack now.



    documentclass{article}

    usepackage{xcolor}

    makeatletter
    begingroupdef:#1{endgroupletswitchhack@sptoken= #1}:{ }
    newcommandswitchhack[1]
    {%
    defswitchhack@switch{#1}%
    switchhack@a
    }
    newcommandswitchhack@a
    {%
    futureletswitchhack@gobbledswitchhack@b
    }
    newcommandswitchhack@b
    {%
    ifxswitchhack@gobbledswitchhack@sptoken
    switchhack@eatspace
    fi
    @firstofone
    {%
    ifxbgroupswitchhack@gobbled
    else
    GenericWarning
    {}{Warning: Argument of stringswitchhackspace not delimited}%
    switchhack@undelimeted
    fi
    switchhack@delimited
    }%
    }
    longdefswitchhack@eatspacefi#1#2%
    {%
    fi
    afterassignmentswitchhack@a
    letswitchhack@gobbled=
    }
    longdefswitchhack@undelimetedfi#1#2%
    {%
    fi
    {switchhack@switch#2}%
    }
    newcommandswitchhack@delimited
    {%
    afterassignmentswitchhack@do
    letswitchhack@gobbled
    }
    newcommandswitchhack@do
    {%
    bgroupswitchhack@switch
    }
    longdefcolorhack#1#{colorhack@{#1}}
    newcommandcolorhack@[2]
    {%
    switchhack{color#1{#2}}%
    }
    makeatother

    newcommandpurp{colorhack{purple}}
    newcommandbfhack{switchhack{bfseries}}

    begin{document}
    colorhack[rgb]{.5 .4 .8}{foo}
    purp{verb|this is blue|}
    following text
    bfhack{this is bold}

    colorhack{red} Undelimeted

    purp{
    begin{verbatim}
    This is
    verbatim
    purple (I lied up there, it never was blue)
    end{verbatim}
    }
    end{document}


    enter image description here






    share|improve this answer















    If it's just about colours, you could fake a textcolor command, which seems to take an argument, but does support verbatim material. Note that this is no general solution, but just provides an argument taking syntax to color which is not really an argument.



    (This way one could use any formatting switches not only colours, e.g. bfseries could be used this way, too)



    EDIT: created a switchhack that allows arbitrary formatting switches not only colours, and colorhack calls switchhack now.



    documentclass{article}

    usepackage{xcolor}

    makeatletter
    begingroupdef:#1{endgroupletswitchhack@sptoken= #1}:{ }
    newcommandswitchhack[1]
    {%
    defswitchhack@switch{#1}%
    switchhack@a
    }
    newcommandswitchhack@a
    {%
    futureletswitchhack@gobbledswitchhack@b
    }
    newcommandswitchhack@b
    {%
    ifxswitchhack@gobbledswitchhack@sptoken
    switchhack@eatspace
    fi
    @firstofone
    {%
    ifxbgroupswitchhack@gobbled
    else
    GenericWarning
    {}{Warning: Argument of stringswitchhackspace not delimited}%
    switchhack@undelimeted
    fi
    switchhack@delimited
    }%
    }
    longdefswitchhack@eatspacefi#1#2%
    {%
    fi
    afterassignmentswitchhack@a
    letswitchhack@gobbled=
    }
    longdefswitchhack@undelimetedfi#1#2%
    {%
    fi
    {switchhack@switch#2}%
    }
    newcommandswitchhack@delimited
    {%
    afterassignmentswitchhack@do
    letswitchhack@gobbled
    }
    newcommandswitchhack@do
    {%
    bgroupswitchhack@switch
    }
    longdefcolorhack#1#{colorhack@{#1}}
    newcommandcolorhack@[2]
    {%
    switchhack{color#1{#2}}%
    }
    makeatother

    newcommandpurp{colorhack{purple}}
    newcommandbfhack{switchhack{bfseries}}

    begin{document}
    colorhack[rgb]{.5 .4 .8}{foo}
    purp{verb|this is blue|}
    following text
    bfhack{this is bold}

    colorhack{red} Undelimeted

    purp{
    begin{verbatim}
    This is
    verbatim
    purple (I lied up there, it never was blue)
    end{verbatim}
    }
    end{document}


    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 8 at 12:43

























    answered May 8 at 11:46









    SkillmonSkillmon

    25k12451




    25k12451













    • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

      – Jaebum
      May 9 at 1:45



















    • I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

      – Jaebum
      May 9 at 1:45

















    I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

    – Jaebum
    May 9 at 1:45





    I also observed weird error without using the verbatim env. I edited a question above. Could you take a look?

    – Jaebum
    May 9 at 1:45











    2














    Another possibility to get macros which take verbatim material as arguments is to grab the arguments as boxes instead. Again this is no universal solution and you can't do arbitrary stuff with it (most importantly you can't change the appearance of the boxed material after it is boxed, that's what the optional arguments of grabbox are for, to make some formatting possible).



    The following defines purp to take its argument inside of a hbox and purp* to take it inside of a vbox (of course the macro name in both cases is purp, the second one is just followed by a star).



    documentclass{article}

    usepackage{grabbox}
    usepackage{xcolor}

    makeatletter
    newsaveboxpurp@box
    newcommandpurp
    {%
    begingroup
    @ifstar
    {%
    grabboxpurp@box[color{purple}]vbox{unvboxpurp@boxendgroup}%
    }
    {%
    grabboxpurp@box[color{purple}]hbox{unhboxpurp@boxendgroup}%
    }%
    }
    makeatother

    begin{document}
    purp{verb|this is blue|}

    other text

    purp*{
    begin{verbatim}
    This is
    verbatim
    purple (I lied up there, it never was blue)
    end{verbatim}
    }
    end{document}


    enter image description here






    share|improve this answer




























      2














      Another possibility to get macros which take verbatim material as arguments is to grab the arguments as boxes instead. Again this is no universal solution and you can't do arbitrary stuff with it (most importantly you can't change the appearance of the boxed material after it is boxed, that's what the optional arguments of grabbox are for, to make some formatting possible).



      The following defines purp to take its argument inside of a hbox and purp* to take it inside of a vbox (of course the macro name in both cases is purp, the second one is just followed by a star).



      documentclass{article}

      usepackage{grabbox}
      usepackage{xcolor}

      makeatletter
      newsaveboxpurp@box
      newcommandpurp
      {%
      begingroup
      @ifstar
      {%
      grabboxpurp@box[color{purple}]vbox{unvboxpurp@boxendgroup}%
      }
      {%
      grabboxpurp@box[color{purple}]hbox{unhboxpurp@boxendgroup}%
      }%
      }
      makeatother

      begin{document}
      purp{verb|this is blue|}

      other text

      purp*{
      begin{verbatim}
      This is
      verbatim
      purple (I lied up there, it never was blue)
      end{verbatim}
      }
      end{document}


      enter image description here






      share|improve this answer


























        2












        2








        2







        Another possibility to get macros which take verbatim material as arguments is to grab the arguments as boxes instead. Again this is no universal solution and you can't do arbitrary stuff with it (most importantly you can't change the appearance of the boxed material after it is boxed, that's what the optional arguments of grabbox are for, to make some formatting possible).



        The following defines purp to take its argument inside of a hbox and purp* to take it inside of a vbox (of course the macro name in both cases is purp, the second one is just followed by a star).



        documentclass{article}

        usepackage{grabbox}
        usepackage{xcolor}

        makeatletter
        newsaveboxpurp@box
        newcommandpurp
        {%
        begingroup
        @ifstar
        {%
        grabboxpurp@box[color{purple}]vbox{unvboxpurp@boxendgroup}%
        }
        {%
        grabboxpurp@box[color{purple}]hbox{unhboxpurp@boxendgroup}%
        }%
        }
        makeatother

        begin{document}
        purp{verb|this is blue|}

        other text

        purp*{
        begin{verbatim}
        This is
        verbatim
        purple (I lied up there, it never was blue)
        end{verbatim}
        }
        end{document}


        enter image description here






        share|improve this answer













        Another possibility to get macros which take verbatim material as arguments is to grab the arguments as boxes instead. Again this is no universal solution and you can't do arbitrary stuff with it (most importantly you can't change the appearance of the boxed material after it is boxed, that's what the optional arguments of grabbox are for, to make some formatting possible).



        The following defines purp to take its argument inside of a hbox and purp* to take it inside of a vbox (of course the macro name in both cases is purp, the second one is just followed by a star).



        documentclass{article}

        usepackage{grabbox}
        usepackage{xcolor}

        makeatletter
        newsaveboxpurp@box
        newcommandpurp
        {%
        begingroup
        @ifstar
        {%
        grabboxpurp@box[color{purple}]vbox{unvboxpurp@boxendgroup}%
        }
        {%
        grabboxpurp@box[color{purple}]hbox{unhboxpurp@boxendgroup}%
        }%
        }
        makeatother

        begin{document}
        purp{verb|this is blue|}

        other text

        purp*{
        begin{verbatim}
        This is
        verbatim
        purple (I lied up there, it never was blue)
        end{verbatim}
        }
        end{document}


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 8 at 12:26









        SkillmonSkillmon

        25k12451




        25k12451






























            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%2f489767%2fmacro-that-takes-any-latex-code-as-an-argument%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

            He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

            Bunad

            Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum