使用 tikz 节点自定义枚举列表计数器

使用 tikz 节点自定义枚举列表计数器

我正在尝试制作一个枚举列表,该列表应使用 tikz 节点和自动编号生成列表,例如问题 1:问题1.1:, 和问题2:

我编写了仅生成单个的 tikz 代码问题我自己就提到了这个号码。

我的代码如下:

\documentclass[a4paper, 12pt]{article}
\usepackage{tikz}
\usepackage[margin=1in]{geometry}


\newcommand{\tb}[1]{\textbf{#1}}

\tikzset{problem/.style={
        fill=red!10,
        text width=1\textwidth-1cm, 
        rounded corners=0.2cm,
        inner sep=0.5cm,
        draw=red!60!black}
}

\newcommand{\problem}[1]
{
    \noindent\tikz \node[problem](instructions) {#1};
    \vspace{0.1cm}
}

\begin{document}
    
    \problem{\tb{Problem 1:} Explain the term Least Significant Bit (LSB).}
    
    \problem{\tb{Problem 2:} Explain the term Most Significant Bit (MSB).}
    
    \problem{\tb{Problem 2.1:} Why MSB is used?}
    
    \problem{\tb{Problem 2.2:} Why LSB is used in x86 architecture?}
    
    \problem{\tb{Problem 3:} Assemble the given program and update the values of the registers after single step execution.}
    
\end{document}

其输出是 问题列表

如何使用枚举环境生成类似的列表,并使用 tikz 节点样式(红色背景颜色和边框)自动编号​​问题 1:,问题 1.1:和问题 2:?

答案1

我会通过 tcolorbox 和两个计数器来实现这一点:

在此处输入图片描述

\documentclass{article}
\usepackage[showframe=false,
margin=1in]{geometry}
\usepackage{varwidth}
\usepackage[most]{tcolorbox}

\newcounter{mainno}
\newcounter{subno}
\newenvironment{myenumerate}
        {\setcounter{mainno}{0}\setcounter{subno}{0}}{}

\pgfmathsetlengthmacro\mysep{4mm}% 4mm = default of tcolorbox for left, right
\tcbset{CommonStyle/.style={
boxrule=0.4pt, % default value TikZ
arc=3pt, 
colframe=red!60!black,   colback=red!10,
top=\mysep, bottom=\mysep,
left=\mysep, right=\mysep,       %boxsep=0mm, 
tcbox width=forced left,
beforeafter skip=1.125\baselineskip, 
},
}

\newtcbox{\subbox}[1][]{CommonStyle, 
before upper={%
\begin{varwidth}{\dimexpr\linewidth-\mysep-\mysep\relax}%
\stepcounter{subno}%
\textbf{Problem \themainno.\thesubno:}~
},     
after upper=\end{varwidth},    #1}

\newtcbox{\mainbox}[1][]{CommonStyle, 
before upper={%
\begin{varwidth}{\dimexpr\linewidth-\mysep-\mysep\relax}%
\stepcounter{mainno}%
\setcounter{subno}{0}%
\textbf{Problem \themainno:}~
},     
after upper=\end{varwidth},   #1}

\begin{document}
\begin{myenumerate}
%  \subbox{Sub item}
  \mainbox{Main item}

  \mainbox{Main item} 
  \subbox{Sub item}
  \subbox{Sub item}

  \mainbox{Main item}
    \subbox{Sub item. This ia a very long text. This ia a very long text. This ia a very long text. This ia a very long text.}
\end{myenumerate}


%\begin{myenumerate}
%  \mainbox{Main item} 
%  \subbox{Sub item}
%   \subbox{Sub item}
%\end{myenumerate}
\end{document}

相关内容