Inserting LaTex Special Characters as xticklabel Values

897 views Asked by At

I'm trying to make a bar chart comparing the distribution of punctuation marks between two datasets. I am trying to get the punctuation mark to appear as the xticklabel for each pair of bars but LaTex keeps assuming I mean to close a pair of $ or throws some other error:

\begin{figure}[h]
\begin{tikzpicture}
    \begin{axis}[
        xtick={1, 2..,25},
        xticklabels={\!, \", \#, \$, \%, \&, \', \(, \), \*, \+, \,, \-, \., \/, \:, \;, \=, \?, \@, \[, \], \`, \|, \textasciitilde},
        ylabel=Year,
        enlargelimits=0.05,
        legend style={at={(0.5,-0.1)},
        anchor=north,legend columns=-1},
        ybar interval=0.7,
    ]
    \addplot
        coordinates {(1,32) (2,4751) (3,57) (4,57) (5,27) (6,49) (7,4198) (8,59) (9,59) (10,29) (11,5) (12,737) (13,1151) (14,390) (15,21) (16,362) (17,2) (18,2) (19,43) (20,6) (21,1) (22,1.0) (23,0) (24,5) (25,20)};
    \addplot
        coordinates {(1,8) (2,390) (3,0) (4,202) (5,117) (6,50) (7,1671) (8,32) (9,32) (10,1) (11,5) (12,3504) (13,1604) (14,2687) (15,60) (16,711) (17,228) (18,1) (19,119) (20,0)  (21,0) (22,0) (23,2) (24,0) (25,0)};
    %\legend{Set1,Set2}
    \end{axis}
\end{tikzpicture}
\end{figure}

As shown above, I have tried escaping the characters, tried escaping only the LaTex special characters, using \verb|| to make them literal (which doesn't work for '|') and I am at a loss for how to approach this.

Thanks for any help!

1

There are 1 answers

0
jf_ On BEST ANSWER

You don't need to escape all of them. In fact, some of the symbols are math spacing commands. The following works:

\documentclass[]{report}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[T1]{fontenc}

\begin{document}

\begin{figure}[h]
\begin{tikzpicture}
    \begin{axis}[
        xtick={1, 2..,25},
        xticklabels={!, ", \#, \$, \%, \&, ', {(}, {)}, *, +, \textbf{,}, -,  ., /, :, ;, =, ?, @, [, ], `, |, \textasciitilde},
        xticklabel style={text height=2ex},
        ylabel=Year,
        enlargelimits=0.05,
        legend style={at={(0.5,-0.1)},
        anchor=north,legend columns=-1},
        ybar interval=0.7,
    ]
    \addplot
        coordinates {(1,32) (2,4751) (3,57) (4,57) (5,27) (6,49) (7,4198) (8,59) (9,59) (10,29) (11,5) (12,737) (13,1151) (14,390) (15,21) (16,362) (17,2) (18,2) (19,43) (20,6) (21,1) (22,1.0) (23,0) (24,5) (25,20)};
    \addplot
        coordinates {(1,8) (2,390) (3,0) (4,202) (5,117) (6,50) (7,1671) (8,32) (9,32) (10,1) (11,5) (12,3504) (13,1604) (14,2687) (15,60) (16,711) (17,228) (18,1) (19,119) (20,0)  (21,0) (22,0) (23,2) (24,0) (25,0)};
    %\legend{Set1,Set2}
    \end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

Giving you this output:

enter image description here

The brackets need to be enclosed in curly brackets. The comma is more tricky, but it prints when it is enclosed in another command, such as textbf. \usepackage[T1]{fontenc} is necessary to print the pipe vertically.

Side note: \verb?|? would work, too, as the first character defines the boundaries.