在另一框内的框内添加文本

在另一框内的框内添加文本

问题:

尝试使用一个颜色框内的文本在另一个颜色框内创建编程代码标签。但是,我不确定将一个框放入另一个框内的最佳做法是什么。

最小工作示例(MWE):

\documentclass{scrreprt}
\usepackage{xcolor}
\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.05}
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\usepackage[framemethod=tikz]{mdframed}

\begin{document}
    \begin{mdframed}[hidealllines=true,backgroundcolor=editorLightGray]
        PHP
    \end{mdframed}
\end{document}

此代码产生以下结果:

在此处输入图片描述

期望的结果: 在此处输入图片描述 更新 在此处输入图片描述

任何指明正确方向的建议都将受到赞赏。

答案1

这是一种使用方法(在后端tcolorbox使用)tikz

\documentclass{scrreprt}
\usepackage{xcolor}
\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.05}
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\usepackage[most]{tcolorbox}
\usepackage{ragged2e}
\usepackage[framemethod=tikz]{mdframed}

\begin{document}
\raggedleft\begin{mdframed}[hidealllines=true,backgroundcolor=editorLightGray]
        PHP
    \end{mdframed}


\begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,colback=editorLightGray,arc=0pt,auto outer arc]
\RaggedLeft\begin{tcolorbox}[boxrule=0pt,coltext=white,colback=editorPurple,arc=0pt,auto outer arc,width=1.5cm,boxsep=0pt,left=5pt,right=5pt,halign=center]
\bfseries PHP
\end{tcolorbox}
\end{tcolorbox} % outer box

\begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,top=2pt,bottom=2pt,colback=editorLightGray,arc=0pt,auto outer arc]
\RaggedLeft\begin{tcolorbox}[boxrule=0pt,coltext=white,colback=magenta,arc=0pt,auto outer arc,width=1.5cm,boxsep=0pt,left=5pt,right=5pt,halign=center]
\bfseries PHP
\end{tcolorbox}
\end{tcolorbox} % outer box


\end{document}

在此处输入图片描述

更新现在作为一个具有许多控制选项等的命令。

\documentclass{scrreprt}
\usepackage{xcolor}
\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.05}
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\usepackage{calc}
\usepackage{xparse}
\usepackage{ragged2e}
\usepackage[most]{tcolorbox}
%\usepackage[framemethod=tikz]{mdframed}

\newlength{\titleboxwidth}

\newtcolorbox{codelanguagebox}[2][]{boxrule=0pt,coltext=white,colback=magenta,arc=0pt,auto outer arc,width={#2},boxsep=0pt,left=5pt,right=5pt,halign=center,#1}

\NewDocumentCommand{\codelabel}{O{}mO{}}{%
  \begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,top=2pt,bottom=2pt,colback=editorLightGray,arc=0pt,auto outer arc,#1]
    \setlength{\titleboxwidth}{\widthof{#2}}%
    \RaggedLeft\begin{codelanguagebox}[add to width=0.5cm,#3]{\the\titleboxwidth}
      \bfseries #2%
    \end{codelanguagebox}%
  \end{tcolorbox}   %
}


\begin{document}

\codelabel{PHP}

\codelabel{MySQL}

\codelabel[colback=blue]{MySQL}[colback=yellow,coltext=black]

\codelabel{C++}

\codelabel{\LaTeXe}


\end{document}

在此处输入图片描述

最后更新

\documentclass{scrreprt}
\usepackage{xcolor}
\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.05}
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\usepackage{calc}
\usepackage{xparse}
\usepackage{ragged2e}
\usepackage[most]{tcolorbox}

\newlength{\titleboxwidth}

\newtcolorbox{codelanguagebox}[2][]{boxrule=0pt,coltext=white,colback=magenta,arc=0pt,auto outer arc,width={#2},boxsep=0pt,halign=center,valign=center,left=5pt,right=5pt,nobeforeafter,#1}

\NewDocumentCommand{\codelabel}{O{}mO{}}{%
  \begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,top=2pt,bottom=2pt,colback=editorLightGray,arc=0pt,auto outer arc,#1]
    \setlength{\titleboxwidth}{\widthof{#2}}%
    \RaggedLeft\begin{codelanguagebox}[add to width=0.5cm,#3]{\the\titleboxwidth}
      \bfseries #2%
    \end{codelanguagebox}%
  \end{tcolorbox}   %
}



\NewDocumentCommand{\twocodelabels}{O{}mO{}mO{}}{%
  \begin{tcolorbox}[boxrule=0pt,boxsep=0pt,right=5pt,top=2pt,bottom=2pt,colback=editorLightGray,arc=0pt,auto outer arc,#1]
    \RaggedLeft%
    \setlength{\topskip}{0pt}%
    \setlength{\titleboxwidth}{\widthof{#2}}%
      \begin{codelanguagebox}[add to width=0.5cm,#3]{\the\titleboxwidth}
      \bfseries #2%
    \end{codelanguagebox} 
    \begin{codelanguagebox}[#3,colback={gray}]{0.75cm}%
      \bfseries +
    \end{codelanguagebox} 
    \setlength{\titleboxwidth}{\widthof{#4}}%
    \begin{codelanguagebox}[#3,add to width=0.5cm,#5]{\the\titleboxwidth}%
      \bfseries #4%
    \end{codelanguagebox}%
\end{tcolorbox}%
}





\begin{document}
\codelabel{MySQL}
\codelabel{\scshape \huge PHP}
\codelabel{MySQL}[colback=editorPurple,coltext=black]
\twocodelabels{\ttfamily HTML}[height=0.6cm,colback=blue]{CSS}[colback=editorPurple]
\end{document}

在此处输入图片描述

相关内容