插入 LaTex 特殊字符作为 xticklabel 值

Inserting LaTex Special Characters as xticklabel Values

我正在尝试制作一个条形图来比较标点符号在两个数据集之间的分布。我试图让标点符号显示为每对条形的 xticklabel 但 LaTex 一直假设我的意思是关闭一对 $ 或抛出一些其他错误:

\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}

如上所示,我试过转义字符,尝试只转义 LaTex 特殊字符,使用 \verb||使它们成为文字(不适用于“|”),我不知道如何处理这个问题。

感谢您的帮助!

你不需要全部转义。事实上,一些符号是数学间距命令。以下作品:

\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}

给你这个输出:

中括号需要用大括号括起来。逗号比较棘手,但是当它包含在另一个命令中时它会打印出来,例如 textbf\usepackage[T1]{fontenc} 是垂直打印管道所必需的。

旁注:\verb?|? 也可以,因为第一个字符定义了边界。