如何使用 \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}

相關內容