我只是想定义一个带有标题框环境的 tcolorbox,如下所示: [I:OBJ_ID_123] 因为它包含下划线。每次输入_ 来显示下划线非常繁琐。因为我的文档包含很多固定格式,如 I:OBJ_ID_123。如何使格式只输入为纯文本而不使用数学符号模式。只需输入为 [I:OBJ_ID_123]。例如
这是演示:
\documentclass[10pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{tcolorbox}
\begin{document}
\begin{tcolorbox}[halign title=flush right,title=\texttt{[I:OBJ\_ID\_123] }]
this is demo for how to define this,i need this effect
\end{tcolorbox}
% start new effect, begin ID:124,125......, it is very hard to type \_ to escape underscore
\begin{tcolorbox}[halign title=flush right,title=\texttt{[I:OBJ\_ID\_124] }]
so the second ID begin 124, i need to type [I:OBJ\_ID\_124]
how to directly to type[I:OBJ_ID_124]
\end{tcolorbox}
\end{document}
答案1
假设只有数字会改变,可以通过定义新命令来实现\obj
:
\documentclass[10pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{tcolorbox}
\begin{document}
\newcommand{\obj}[1]{I:OBJ\_ID\_#1}
\begin{tcolorbox}[halign title=flush right,title=\texttt{[I:OBJ\_ID\_123] }]
this is demo for how to define this,i need this effect
\end{tcolorbox}
% start new effect, begin ID:124,125......, it is very hard to type \_ to escape underscore
\begin{tcolorbox}[halign title=flush right,title=\texttt{[I:OBJ\_ID\_124] }]
so the second ID begin 124, i need to type [I:OBJ\_ID\_124]
% how to directly to type[I:OBJ_ID_124]
\end{tcolorbox}
\begin{tcolorbox}[halign title=flush right,title=\texttt{[\obj{456}] }]
using a macro (newcommand) to insert the digits
\end{tcolorbox}
\end{document}
结果:
有关\newcommand
和的更多详细信息\newenvironment
请参见此处,例如吸收标题和 tcolorbox 语句:https://en.wikibooks.org/wiki/LaTeX/Macros。