如何使用 \newtcbinputlisting 创建逐字框?

如何使用 \newtcbinputlisting 创建逐字框?

我想要定义一个类似的命令\maplebox;例如命令\maplebox{\latex}

为什么下面的代码不能编译?

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{lipsum}
 \usepackage[top=2.5cm,right=3.5cm,bottom=2.5cm,left=2.5cm]{geometry}      

\tcbuselibrary{listings,skins,theorems}

\newtcbinputlisting{\maplebox}[1][white]{
                                                            listing only,
                                                            nobeforeafter,
                                                            tcbox raise base,
%                                                           fontupper=\latinfont\setLTR\ttfamily\bfseries,
                                                            fontupper=\ttfamily\bfseries,
                                                            arc=0pt,
                                                            outer arc=0pt,
                                                            colback=#1!10!white,
                                                            colframe=#1!50!black,
                                                            boxsep=0pt,
                                                            left=1pt,
                                                            right=1pt,
                                                            top=2pt,
                                                            bottom=2pt,
                                                            boxrule=0pt,
                                                            bottomrule=1pt,
                                                            toprule=1pt}


\begin{document}

The \maplebox{\latex}

\end{document}

答案1

有几个问题/注意事项。首先,\newtcbinputlisting创建一个新的列表输入命令,但如果我说得没错的话,您不想读取文件。所以,\newtcblisting应该使用。这会创建一个逐字环境,而不是宏。不建议仅使用可选参数来使用这样的环境。如果您有一个强制参数,那么您也可以使用可选参数。

现在,我稍微改变了一下你的例子,希望它对你有用:

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{lipsum}
\usepackage[top=2.5cm,right=3.5cm,bottom=2.5cm,left=2.5cm]{geometry}

\tcbuselibrary{listings,skins,theorems}

\newtcblisting{maplebox}[1]{
  listing only,
  colupper=black,
  nobeforeafter,
  hbox,
  tcbox raise base,
  fontupper=\ttfamily\bfseries,
  arc=0pt,
  outer arc=0pt,
  colback=#1!10!white,
  colframe=#1!50!black,
  boxsep=0pt,
  left=1pt,
  right=1pt,
  top=2pt,
  bottom=2pt,
  boxrule=0pt,
  bottomrule=1pt,
  toprule=1pt,
  }

\begin{document}

The
\begin{maplebox}{white}
\latex
\end{maplebox}
\
\begin{maplebox}{red}
\latex
\end{maplebox}
\
\begin{maplebox}{blue}
\latex
\end{maplebox}
\
\begin{maplebox}{green}
\latex
\end{maplebox}
\ test.

\end{document}

在此处输入图片描述

更新: 借助xparse包,可以实现宏解决方案。它使用嵌入在from 包中的\lstinlinefrom 包。格式化选项直接设置为。listings\tcboxtcolorbox\lstinline

\documentclass{article}

\usepackage{xparse}
\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{lipsum}
\usepackage[top=2.5cm,right=3.5cm,bottom=2.5cm,left=2.5cm]{geometry}

\tcbuselibrary{listings,skins,theorems}

\NewDocumentCommand{\maplebox}{ O{white} v}{%
  \tcbox[colupper=black,nobeforeafter,tcbox raise base,
    arc=0pt,outer arc=0pt,colback=#1!10!white,
    colframe=#1!50!black,
    boxsep=0pt,left=1pt,right=1pt,top=2pt,bottom=2pt,
    boxrule=0pt,bottomrule=1pt,toprule=1pt]{%
  \lstinline[style=tcblatex,texcsstyle=*\color{blue}\bfseries]§#2§}}

\begin{document}

\maplebox{\LaTeX}
\maplebox[red]{Some \LaTeX\ code}
\maplebox[blue]{$a^2+b^2=c^2$}
\maplebox[green]{\textbf{something}}

\end{document}

在此处输入图片描述

最后更新:tcolorbox可以使用from version以更优雅的语法生成相同的输出2.80 (2014/03/31)

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{lipsum}
\usepackage[top=2.5cm,right=3.5cm,bottom=2.5cm,left=2.5cm]{geometry}

\tcbuselibrary{listings,skins,theorems,xparse}

\NewTotalTCBox{\maplebox}{ O{white} v}
{ colupper=black,nobeforeafter,tcbox raise base,
  arc=0pt,outer arc=0pt,colback=#1!10!white,
  colframe=#1!50!black,
  boxsep=0pt,left=1pt,right=1pt,top=2pt,bottom=2pt,
  boxrule=0pt,bottomrule=1pt,toprule=1pt}
{\lstinline[style=tcblatex,texcsstyle=*\color{blue}\bfseries]§#2§}

\begin{document}

\maplebox{\LaTeX}
\maplebox[red]{Some \LaTeX\ code}
\maplebox[blue]{$a^2+b^2=c^2$}
\maplebox[green]{\textbf{something}}

\end{document}

相关内容