Escape character in LaTeX











up vote
114
down vote

favorite
46












I need to output the below text but since is a special character, I cannot:



[RegularExpression(@"d+")]


Also sometimes I need use dollar sign $ as well but it seems to be a special char, too.



What is the way to escape those in LaTeX?



UPDATE:



I used verb as suggested but I am unable to run commands inside it. Also, in the output the font and the opacity of the text is different than the default one:



verb|[RegularExpression(newline @"d+")]|









share|improve this question




















  • 2




    In short: cespedes.org/blog/85/how-to-escape-latex-special-characters
    – caw
    Oct 10 '13 at 0:09















up vote
114
down vote

favorite
46












I need to output the below text but since is a special character, I cannot:



[RegularExpression(@"d+")]


Also sometimes I need use dollar sign $ as well but it seems to be a special char, too.



What is the way to escape those in LaTeX?



UPDATE:



I used verb as suggested but I am unable to run commands inside it. Also, in the output the font and the opacity of the text is different than the default one:



verb|[RegularExpression(newline @"d+")]|









share|improve this question




















  • 2




    In short: cespedes.org/blog/85/how-to-escape-latex-special-characters
    – caw
    Oct 10 '13 at 0:09













up vote
114
down vote

favorite
46









up vote
114
down vote

favorite
46






46





I need to output the below text but since is a special character, I cannot:



[RegularExpression(@"d+")]


Also sometimes I need use dollar sign $ as well but it seems to be a special char, too.



What is the way to escape those in LaTeX?



UPDATE:



I used verb as suggested but I am unable to run commands inside it. Also, in the output the font and the opacity of the text is different than the default one:



verb|[RegularExpression(newline @"d+")]|









share|improve this question















I need to output the below text but since is a special character, I cannot:



[RegularExpression(@"d+")]


Also sometimes I need use dollar sign $ as well but it seems to be a special char, too.



What is the way to escape those in LaTeX?



UPDATE:



I used verb as suggested but I am unable to run commands inside it. Also, in the output the font and the opacity of the text is different than the default one:



verb|[RegularExpression(newline @"d+")]|






symbols tex-core characters






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 '12 at 0:27









lockstep

189k52585719




189k52585719










asked Nov 12 '11 at 15:55









tugberk

1,31741824




1,31741824








  • 2




    In short: cespedes.org/blog/85/how-to-escape-latex-special-characters
    – caw
    Oct 10 '13 at 0:09














  • 2




    In short: cespedes.org/blog/85/how-to-escape-latex-special-characters
    – caw
    Oct 10 '13 at 0:09








2




2




In short: cespedes.org/blog/85/how-to-escape-latex-special-characters
– caw
Oct 10 '13 at 0:09




In short: cespedes.org/blog/85/how-to-escape-latex-special-characters
– caw
Oct 10 '13 at 0:09










3 Answers
3






active

oldest

votes

















up vote
181
down vote



accepted










The following ten characters have special meanings in (La)TeX:




& % $ # _ { } ~ ^




Outside verb, the first seven of them can be typeset by prepending a backslash; for the other three, use the macros textasciitilde, textasciicircum, and textbackslash.



documentclass{article}

begin{document}

& % $ # _ { }

textasciitilde

textasciicircum

textbackslash

end{document}


enter image description here



Note that the seven "single non-letter" macros don't gobble the space following them.
For the last three that do gobble up the space after them you can try one of these methods to add space.






share|improve this answer























  • In pdfLaTeX I have defined a new command textampersand by using newcommand*{textampersand}[0]{&} or renewcommand*{textampersand}[0]{&}. In XeLaTeX it is already available, I think.
    – matth
    May 9 '12 at 17:35












  • In terms of preventing characters gobbling the space after them, you can use usepackage{xspace} and then xspace directly after the character that might gobble the space to prevent this.
    – Savara
    Dec 1 '15 at 12:24










  • It's worth adding that [ (and ]) also have a special meaning in certain circumstances. For example item [a]bc will not print [a]bc (as it will be considered as an option passed to the command).
    – kebs
    Nov 11 at 8:00


















up vote
26
down vote













Usually text like that is typeset in typewriter type and so there's a slick way to arrange it



verb|[RegularExpression(@"d+")]|


After verb should go a character that's not used in the text to print "verbatim" and the same character should follow the text.



This command has a drawback: it can't be used in the argument of other commands.



There's a second "solution" which can come handy if it's needed a limited number of times:



texttt{[RegularExpression(@"stringd+")]}


where commands inside the argument to texttt are allowed. It's not even necessary to use texttt:



textsf{[RegularExpression(@"stringd+")]}


will work as well (when T1 font encoding is active) and will print the string in sans serif type.






share|improve this answer



















  • 2




    first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :)
    – tugberk
    Nov 12 '11 at 16:22










  • Would you please modify your question and show some more cases?
    – egreg
    Nov 12 '11 at 16:27










  • sure, see it. I updated!
    – tugberk
    Nov 12 '11 at 16:31






  • 2




    @egreg: in your definition of pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions).
    – Philippe Goutet
    Nov 12 '11 at 22:11










  • @PhilippeGoutet Yes, you're right; I've deleted that part
    – egreg
    Nov 12 '11 at 22:14


















up vote
10
down vote













I needed a way to escape all special characters and I found this Perl function:



sub latex_escape {
my $paragraph = shift;

# Replace a with $backslash$
# This is made more complicated because the dollars will be escaped
# by the subsequent replacement. Easiest to add backslash
# now and then add the dollars
$paragraph =~ s/\/\backslash/g;

# Must be done after escape of since this command adds latex escapes
# Replace characters that can be escaped
$paragraph =~ s/([$#&%_{}])/\$1/g;

# Replace ^ characters with ^{} so that $^F works okay
$paragraph =~ s/(^)/\$1{}/g;

# Replace tilde (~) with texttt{~{}}
$paragraph =~ s/~/\texttt{\~{}}/g;

# Now add the dollars around each backslash
$paragraph =~ s/(\backslash)/$$1$/g;
return $paragraph;
}


For example it will convert this:



& % $ # _ { } ~ ^  today


into this:



& % $ # _ {  } texttt{~{}} ^{} $backslash$ $backslash$today





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',
    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%2f34580%2fescape-character-in-latex%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








    up vote
    181
    down vote



    accepted










    The following ten characters have special meanings in (La)TeX:




    & % $ # _ { } ~ ^




    Outside verb, the first seven of them can be typeset by prepending a backslash; for the other three, use the macros textasciitilde, textasciicircum, and textbackslash.



    documentclass{article}

    begin{document}

    & % $ # _ { }

    textasciitilde

    textasciicircum

    textbackslash

    end{document}


    enter image description here



    Note that the seven "single non-letter" macros don't gobble the space following them.
    For the last three that do gobble up the space after them you can try one of these methods to add space.






    share|improve this answer























    • In pdfLaTeX I have defined a new command textampersand by using newcommand*{textampersand}[0]{&} or renewcommand*{textampersand}[0]{&}. In XeLaTeX it is already available, I think.
      – matth
      May 9 '12 at 17:35












    • In terms of preventing characters gobbling the space after them, you can use usepackage{xspace} and then xspace directly after the character that might gobble the space to prevent this.
      – Savara
      Dec 1 '15 at 12:24










    • It's worth adding that [ (and ]) also have a special meaning in certain circumstances. For example item [a]bc will not print [a]bc (as it will be considered as an option passed to the command).
      – kebs
      Nov 11 at 8:00















    up vote
    181
    down vote



    accepted










    The following ten characters have special meanings in (La)TeX:




    & % $ # _ { } ~ ^




    Outside verb, the first seven of them can be typeset by prepending a backslash; for the other three, use the macros textasciitilde, textasciicircum, and textbackslash.



    documentclass{article}

    begin{document}

    & % $ # _ { }

    textasciitilde

    textasciicircum

    textbackslash

    end{document}


    enter image description here



    Note that the seven "single non-letter" macros don't gobble the space following them.
    For the last three that do gobble up the space after them you can try one of these methods to add space.






    share|improve this answer























    • In pdfLaTeX I have defined a new command textampersand by using newcommand*{textampersand}[0]{&} or renewcommand*{textampersand}[0]{&}. In XeLaTeX it is already available, I think.
      – matth
      May 9 '12 at 17:35












    • In terms of preventing characters gobbling the space after them, you can use usepackage{xspace} and then xspace directly after the character that might gobble the space to prevent this.
      – Savara
      Dec 1 '15 at 12:24










    • It's worth adding that [ (and ]) also have a special meaning in certain circumstances. For example item [a]bc will not print [a]bc (as it will be considered as an option passed to the command).
      – kebs
      Nov 11 at 8:00













    up vote
    181
    down vote



    accepted







    up vote
    181
    down vote



    accepted






    The following ten characters have special meanings in (La)TeX:




    & % $ # _ { } ~ ^




    Outside verb, the first seven of them can be typeset by prepending a backslash; for the other three, use the macros textasciitilde, textasciicircum, and textbackslash.



    documentclass{article}

    begin{document}

    & % $ # _ { }

    textasciitilde

    textasciicircum

    textbackslash

    end{document}


    enter image description here



    Note that the seven "single non-letter" macros don't gobble the space following them.
    For the last three that do gobble up the space after them you can try one of these methods to add space.






    share|improve this answer














    The following ten characters have special meanings in (La)TeX:




    & % $ # _ { } ~ ^




    Outside verb, the first seven of them can be typeset by prepending a backslash; for the other three, use the macros textasciitilde, textasciicircum, and textbackslash.



    documentclass{article}

    begin{document}

    & % $ # _ { }

    textasciitilde

    textasciicircum

    textbackslash

    end{document}


    enter image description here



    Note that the seven "single non-letter" macros don't gobble the space following them.
    For the last three that do gobble up the space after them you can try one of these methods to add space.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 13 '17 at 12:35









    Community

    1




    1










    answered Nov 12 '11 at 16:41









    lockstep

    189k52585719




    189k52585719












    • In pdfLaTeX I have defined a new command textampersand by using newcommand*{textampersand}[0]{&} or renewcommand*{textampersand}[0]{&}. In XeLaTeX it is already available, I think.
      – matth
      May 9 '12 at 17:35












    • In terms of preventing characters gobbling the space after them, you can use usepackage{xspace} and then xspace directly after the character that might gobble the space to prevent this.
      – Savara
      Dec 1 '15 at 12:24










    • It's worth adding that [ (and ]) also have a special meaning in certain circumstances. For example item [a]bc will not print [a]bc (as it will be considered as an option passed to the command).
      – kebs
      Nov 11 at 8:00


















    • In pdfLaTeX I have defined a new command textampersand by using newcommand*{textampersand}[0]{&} or renewcommand*{textampersand}[0]{&}. In XeLaTeX it is already available, I think.
      – matth
      May 9 '12 at 17:35












    • In terms of preventing characters gobbling the space after them, you can use usepackage{xspace} and then xspace directly after the character that might gobble the space to prevent this.
      – Savara
      Dec 1 '15 at 12:24










    • It's worth adding that [ (and ]) also have a special meaning in certain circumstances. For example item [a]bc will not print [a]bc (as it will be considered as an option passed to the command).
      – kebs
      Nov 11 at 8:00
















    In pdfLaTeX I have defined a new command textampersand by using newcommand*{textampersand}[0]{&} or renewcommand*{textampersand}[0]{&}. In XeLaTeX it is already available, I think.
    – matth
    May 9 '12 at 17:35






    In pdfLaTeX I have defined a new command textampersand by using newcommand*{textampersand}[0]{&} or renewcommand*{textampersand}[0]{&}. In XeLaTeX it is already available, I think.
    – matth
    May 9 '12 at 17:35














    In terms of preventing characters gobbling the space after them, you can use usepackage{xspace} and then xspace directly after the character that might gobble the space to prevent this.
    – Savara
    Dec 1 '15 at 12:24




    In terms of preventing characters gobbling the space after them, you can use usepackage{xspace} and then xspace directly after the character that might gobble the space to prevent this.
    – Savara
    Dec 1 '15 at 12:24












    It's worth adding that [ (and ]) also have a special meaning in certain circumstances. For example item [a]bc will not print [a]bc (as it will be considered as an option passed to the command).
    – kebs
    Nov 11 at 8:00




    It's worth adding that [ (and ]) also have a special meaning in certain circumstances. For example item [a]bc will not print [a]bc (as it will be considered as an option passed to the command).
    – kebs
    Nov 11 at 8:00










    up vote
    26
    down vote













    Usually text like that is typeset in typewriter type and so there's a slick way to arrange it



    verb|[RegularExpression(@"d+")]|


    After verb should go a character that's not used in the text to print "verbatim" and the same character should follow the text.



    This command has a drawback: it can't be used in the argument of other commands.



    There's a second "solution" which can come handy if it's needed a limited number of times:



    texttt{[RegularExpression(@"stringd+")]}


    where commands inside the argument to texttt are allowed. It's not even necessary to use texttt:



    textsf{[RegularExpression(@"stringd+")]}


    will work as well (when T1 font encoding is active) and will print the string in sans serif type.






    share|improve this answer



















    • 2




      first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :)
      – tugberk
      Nov 12 '11 at 16:22










    • Would you please modify your question and show some more cases?
      – egreg
      Nov 12 '11 at 16:27










    • sure, see it. I updated!
      – tugberk
      Nov 12 '11 at 16:31






    • 2




      @egreg: in your definition of pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions).
      – Philippe Goutet
      Nov 12 '11 at 22:11










    • @PhilippeGoutet Yes, you're right; I've deleted that part
      – egreg
      Nov 12 '11 at 22:14















    up vote
    26
    down vote













    Usually text like that is typeset in typewriter type and so there's a slick way to arrange it



    verb|[RegularExpression(@"d+")]|


    After verb should go a character that's not used in the text to print "verbatim" and the same character should follow the text.



    This command has a drawback: it can't be used in the argument of other commands.



    There's a second "solution" which can come handy if it's needed a limited number of times:



    texttt{[RegularExpression(@"stringd+")]}


    where commands inside the argument to texttt are allowed. It's not even necessary to use texttt:



    textsf{[RegularExpression(@"stringd+")]}


    will work as well (when T1 font encoding is active) and will print the string in sans serif type.






    share|improve this answer



















    • 2




      first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :)
      – tugberk
      Nov 12 '11 at 16:22










    • Would you please modify your question and show some more cases?
      – egreg
      Nov 12 '11 at 16:27










    • sure, see it. I updated!
      – tugberk
      Nov 12 '11 at 16:31






    • 2




      @egreg: in your definition of pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions).
      – Philippe Goutet
      Nov 12 '11 at 22:11










    • @PhilippeGoutet Yes, you're right; I've deleted that part
      – egreg
      Nov 12 '11 at 22:14













    up vote
    26
    down vote










    up vote
    26
    down vote









    Usually text like that is typeset in typewriter type and so there's a slick way to arrange it



    verb|[RegularExpression(@"d+")]|


    After verb should go a character that's not used in the text to print "verbatim" and the same character should follow the text.



    This command has a drawback: it can't be used in the argument of other commands.



    There's a second "solution" which can come handy if it's needed a limited number of times:



    texttt{[RegularExpression(@"stringd+")]}


    where commands inside the argument to texttt are allowed. It's not even necessary to use texttt:



    textsf{[RegularExpression(@"stringd+")]}


    will work as well (when T1 font encoding is active) and will print the string in sans serif type.






    share|improve this answer














    Usually text like that is typeset in typewriter type and so there's a slick way to arrange it



    verb|[RegularExpression(@"d+")]|


    After verb should go a character that's not used in the text to print "verbatim" and the same character should follow the text.



    This command has a drawback: it can't be used in the argument of other commands.



    There's a second "solution" which can come handy if it's needed a limited number of times:



    texttt{[RegularExpression(@"stringd+")]}


    where commands inside the argument to texttt are allowed. It's not even necessary to use texttt:



    textsf{[RegularExpression(@"stringd+")]}


    will work as well (when T1 font encoding is active) and will print the string in sans serif type.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 '12 at 21:26

























    answered Nov 12 '11 at 16:04









    egreg

    703k8618743151




    703k8618743151








    • 2




      first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :)
      – tugberk
      Nov 12 '11 at 16:22










    • Would you please modify your question and show some more cases?
      – egreg
      Nov 12 '11 at 16:27










    • sure, see it. I updated!
      – tugberk
      Nov 12 '11 at 16:31






    • 2




      @egreg: in your definition of pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions).
      – Philippe Goutet
      Nov 12 '11 at 22:11










    • @PhilippeGoutet Yes, you're right; I've deleted that part
      – egreg
      Nov 12 '11 at 22:14














    • 2




      first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :)
      – tugberk
      Nov 12 '11 at 16:22










    • Would you please modify your question and show some more cases?
      – egreg
      Nov 12 '11 at 16:27










    • sure, see it. I updated!
      – tugberk
      Nov 12 '11 at 16:31






    • 2




      @egreg: in your definition of pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions).
      – Philippe Goutet
      Nov 12 '11 at 22:11










    • @PhilippeGoutet Yes, you're right; I've deleted that part
      – egreg
      Nov 12 '11 at 22:14








    2




    2




    first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :)
    – tugberk
    Nov 12 '11 at 16:22




    first solution is nice but I cannot use commands inside it. second solution breaks my entire document. Does every single little thing have to be so hard in LaTeX :)
    – tugberk
    Nov 12 '11 at 16:22












    Would you please modify your question and show some more cases?
    – egreg
    Nov 12 '11 at 16:27




    Would you please modify your question and show some more cases?
    – egreg
    Nov 12 '11 at 16:27












    sure, see it. I updated!
    – tugberk
    Nov 12 '11 at 16:31




    sure, see it. I updated!
    – tugberk
    Nov 12 '11 at 16:31




    2




    2




    @egreg: in your definition of pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions).
    – Philippe Goutet
    Nov 12 '11 at 22:11




    @egreg: in your definition of pseudoverb, you've got [1} instead of [1]. And a drawback you don't mention of detokenize is that it inserts spaces after macros names with more than one character (not sure if it's relevant for regular expressions).
    – Philippe Goutet
    Nov 12 '11 at 22:11












    @PhilippeGoutet Yes, you're right; I've deleted that part
    – egreg
    Nov 12 '11 at 22:14




    @PhilippeGoutet Yes, you're right; I've deleted that part
    – egreg
    Nov 12 '11 at 22:14










    up vote
    10
    down vote













    I needed a way to escape all special characters and I found this Perl function:



    sub latex_escape {
    my $paragraph = shift;

    # Replace a with $backslash$
    # This is made more complicated because the dollars will be escaped
    # by the subsequent replacement. Easiest to add backslash
    # now and then add the dollars
    $paragraph =~ s/\/\backslash/g;

    # Must be done after escape of since this command adds latex escapes
    # Replace characters that can be escaped
    $paragraph =~ s/([$#&%_{}])/\$1/g;

    # Replace ^ characters with ^{} so that $^F works okay
    $paragraph =~ s/(^)/\$1{}/g;

    # Replace tilde (~) with texttt{~{}}
    $paragraph =~ s/~/\texttt{\~{}}/g;

    # Now add the dollars around each backslash
    $paragraph =~ s/(\backslash)/$$1$/g;
    return $paragraph;
    }


    For example it will convert this:



    & % $ # _ { } ~ ^  today


    into this:



    & % $ # _ {  } texttt{~{}} ^{} $backslash$ $backslash$today





    share|improve this answer



























      up vote
      10
      down vote













      I needed a way to escape all special characters and I found this Perl function:



      sub latex_escape {
      my $paragraph = shift;

      # Replace a with $backslash$
      # This is made more complicated because the dollars will be escaped
      # by the subsequent replacement. Easiest to add backslash
      # now and then add the dollars
      $paragraph =~ s/\/\backslash/g;

      # Must be done after escape of since this command adds latex escapes
      # Replace characters that can be escaped
      $paragraph =~ s/([$#&%_{}])/\$1/g;

      # Replace ^ characters with ^{} so that $^F works okay
      $paragraph =~ s/(^)/\$1{}/g;

      # Replace tilde (~) with texttt{~{}}
      $paragraph =~ s/~/\texttt{\~{}}/g;

      # Now add the dollars around each backslash
      $paragraph =~ s/(\backslash)/$$1$/g;
      return $paragraph;
      }


      For example it will convert this:



      & % $ # _ { } ~ ^  today


      into this:



      & % $ # _ {  } texttt{~{}} ^{} $backslash$ $backslash$today





      share|improve this answer

























        up vote
        10
        down vote










        up vote
        10
        down vote









        I needed a way to escape all special characters and I found this Perl function:



        sub latex_escape {
        my $paragraph = shift;

        # Replace a with $backslash$
        # This is made more complicated because the dollars will be escaped
        # by the subsequent replacement. Easiest to add backslash
        # now and then add the dollars
        $paragraph =~ s/\/\backslash/g;

        # Must be done after escape of since this command adds latex escapes
        # Replace characters that can be escaped
        $paragraph =~ s/([$#&%_{}])/\$1/g;

        # Replace ^ characters with ^{} so that $^F works okay
        $paragraph =~ s/(^)/\$1{}/g;

        # Replace tilde (~) with texttt{~{}}
        $paragraph =~ s/~/\texttt{\~{}}/g;

        # Now add the dollars around each backslash
        $paragraph =~ s/(\backslash)/$$1$/g;
        return $paragraph;
        }


        For example it will convert this:



        & % $ # _ { } ~ ^  today


        into this:



        & % $ # _ {  } texttt{~{}} ^{} $backslash$ $backslash$today





        share|improve this answer














        I needed a way to escape all special characters and I found this Perl function:



        sub latex_escape {
        my $paragraph = shift;

        # Replace a with $backslash$
        # This is made more complicated because the dollars will be escaped
        # by the subsequent replacement. Easiest to add backslash
        # now and then add the dollars
        $paragraph =~ s/\/\backslash/g;

        # Must be done after escape of since this command adds latex escapes
        # Replace characters that can be escaped
        $paragraph =~ s/([$#&%_{}])/\$1/g;

        # Replace ^ characters with ^{} so that $^F works okay
        $paragraph =~ s/(^)/\$1{}/g;

        # Replace tilde (~) with texttt{~{}}
        $paragraph =~ s/~/\texttt{\~{}}/g;

        # Now add the dollars around each backslash
        $paragraph =~ s/(\backslash)/$$1$/g;
        return $paragraph;
        }


        For example it will convert this:



        & % $ # _ { } ~ ^  today


        into this:



        & % $ # _ {  } texttt{~{}} ^{} $backslash$ $backslash$today






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 2 at 23:37









        Andrew

        30.3k34380




        30.3k34380










        answered Jun 16 '13 at 11:44









        ypid

        77179




        77179






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f34580%2fescape-character-in-latex%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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?