用 tikz 画一个特殊的框

用 tikz 画一个特殊的框

通过从那个优秀的代码中获得启发问题 我想画一些类似的东西

在此处输入图片描述

但由于疲惫,我只能画出这个

输出:

在此处输入图片描述

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{tikz}

\usetikzlibrary{calc,positioning}

\newcommand{\mytab}[3]{
\begin{tikzpicture}
        \node[anchor=text, text=#2] (#1) {\raisebox{1mm}{\textbf{\emph{#1}}}};  
        \node[anchor=text,text width=\textwidth] (sec) {\hphantom{\textbf{\emph{#1}}} \hspace{.2em} #3};
        \draw[draw=#2] (#1.north east|-sec.north) -- ($(#1.south east)+(0,2pt)$) -- ($(#1.south west)+(0,2pt)$) -- 
                    (sec.south west) -- (sec.south east) -- (sec.north east);

                \draw[draw=#2] (#1.south east|-sec.north) -- ($(#1.south east)+(0,2pt)$) -- ($(#1.south west)+(0,2pt)$) -- 
                    (sec.north west) -- (sec.north east) -- (sec.north east);
\end{tikzpicture}       
}

\begin{document}

    \mytab{a}{green}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.}

    \mytab{Text}{blue}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.}

    \mytab{Very Long Text}{red}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.}

    \mytab{Corollario}{purple}{Sia ($X,T$) spazio topologico.\\
        Equation goes here
    }
\end{document}
  • 另外我想在标题中添加反义词,你能帮我吗

答案1

当其他人正在研究tcolorbox解决方案时(我也认为这个解决方案比这个更好),你可以使用 Alenanno 的代码并附加以下内容--cycle

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{tikz}

\usetikzlibrary{calc,positioning}

\newcounter{mytabcounter}
\newcommand{\mytab}[3]{
\addtocounter{mytabcounter}{1}
\begin{tikzpicture}
        \node[anchor=text, text=#2] (#1) {\raisebox{1mm}{\textbf{\emph{#1: \themytabcounter}}}};  
        \node[anchor=text,text width=\textwidth] (sec)
        {\hphantom{\textbf{\emph{#1: \themytabcounter}}} \hspace{.2em} #3};
        \draw[draw=#2] (#1.north east|-sec.north) -- ($(#1.south east)+(0,2pt)$) -- 
           ($(#1.south west)+(0,2pt)$) -- (sec.south west) -- 
           (sec.south east) -- (sec.north east)--cycle;
\end{tikzpicture}       
}

\begin{document}

    \mytab{a}{green}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.}

    \mytab{Text}{blue}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.}

    \mytab{Very Long Text}{red}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.}

    \mytab{Corollario}{purple}{Sia ($X,T$) spazio topologico.\\
        Equation goes here
    }
\end{document}

在此处输入图片描述

答案2

我设法想出了一个tcolorbox解决方案,我认为它比TiKZ这个问题更好。但由于 OP 没有提到,我宁愿写一个独立的答案,而不是编辑我之前的答案,这确实解决了原始问题。如果有人认为我应该把它添加到我之前的答案中,请告诉我。

以下代码声明了一个带有两个参数的tcolorbox调用mytctab,第一个是框名称,第二个是框架颜色。可以使用可选参数,如上例所示。这些框有自己的计数器,会自动添加到标题后面。

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}

\newtcolorbox[auto counter]{mytctab}[3][]{%
enhanced,
breakable,
size=small,
sharp corners,
colback=white,
colbacktitle=white,
fonttitle=\bfseries\itshape,
coltitle=#3,
colframe=#3,
title=\strut#2:~\thetcbcounter,
boxed title style={size=small, colframe=white},
attach boxed title to top left={yshift=-\tcboxedtitleheight*7/10},
before upper=\hspace{\tcboxedtitlewidth},
frame hidden,
overlay={\draw[#3] (frame.south west)--(title.south west)-|
   (title.north east|-frame.north)-|(frame.south east)--cycle;},
#1
}

\begin{document}

\lipsum[4]

\begin{mytctab}{a}{green}
\lipsum[1]
\end{mytctab}

\begin{mytctab}{Text}{blue}
\lipsum[2]
\end{mytctab}

\begin{mytctab}{Very Long Text}{red}
\lipsum[3]
\end{mytctab}

\begin{mytctab}[colback=purple!20]{Corollario}{purple}
Sia ($X,T$) spazio topologico.\\
\[\sin^2 x + \cos^2 x = 1\]
\end{mytctab}

\end{document}

在此处输入图片描述

答案3

根据 Ignasi 的第一个(优秀)答案,我发布了我自己的版本,我根据自己的目的对其进行了修改。主要变化如下

  • 我已经把\mytab 命令变成环境。这在排版方面提供了更大的自由度(请参见下面的示例\obeylines)。
  • 我用mytabounter计数器来命名sec节点——TiZ 不喜欢用重音字符作为标签。
  • 我稍微改变了轮廓的绘制以简化更改并减少框架顶部的空间。
  • 我已将标签文本放入其中,\sbox以简化一些事情。
  • 排版框现在精确地位于 内\textwidth
  • 它们rounded corners纯粹是装饰性的,可以省略。

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{xparse}

\usetikzlibrary{calc}

\newcounter{mytabcounter}
\newsavebox{\secbox}

\NewDocumentEnvironment{mytab}{mm}{%
    \stepcounter{mytabcounter}
    \sbox{\secbox}{\textcolor{#2}{\textbf{\large\strut\emph{#1: \themytabcounter}}}}
    \noindent
    \begin{tikzpicture}[ultra thick]
        \node[anchor=text] (sec-\themytabcounter) {\raisebox{1pt}{\usebox{\secbox}}};  
        \node[anchor=text,
            text width=\textwidth-2*\pgfkeysvalueof{/pgf/inner xsep}-2*\pgflinewidth] (sec)
        \bgroup
            \baselineskip1.4em %% See below *
            \hphantom{\usebox{\secbox}}%
            \hspace{0.75em}
            \ignorespaces
}{%
        \par
        \egroup;
        \draw[draw=#2,rounded corners=2pt] 
            %% * The 3pt can be altered to adjust for a change in \baselineskip
            ($(sec-\themytabcounter.south west)+(0,3pt)$)coordinate(Y) -- 
            (sec.south west) -- 
            (sec.south east) -- 
            %% Change the -3pt as needed to reduce space at the top of the box
            ($(sec.north east |- sec-\themytabcounter.north east) + (0,-3pt)$) --
            ($(sec-\themytabcounter.north east) + (0,-3pt)$) --
            (sec-\themytabcounter.north east |- Y) -- cycle;
    \end{tikzpicture}%
}

\begin{document}

    \begin{mytab}{a}{green!75!black}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.


    \begingroup
    \obeylines
    1
    2
    3
    4
    5
    \endgroup
    \end{mytab}

    \begin{mytab}{Text}{blue}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.       \end{mytab}

    \begin{mytab}{Very Long Text}{red}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed semper dui in tellus hendrerit semper. Aenean in rutrum nulla. Nullam eleifend feugiat neque, sit amet iaculis arcu aliquam id. Vestibulum maximus, risus at condimentum tempus, ipsum nunc suscipit velit, at tempus nisi arcu nec ipsum. Morbi eget tortor in turpis ultricies condimentum. Sed venenatis condimentum feugiat. Integer eget ligula sem. Sed gravida laoreet ultrices. Vivamus tincidunt ac metus nec finibus. Praesent a felis sed enim mollis ornare.       \end{mytab}

    \begin{mytab}{Corollario}{purple}
        Sia ($X,T$) spazio topologico.\\
        Equation goes here
    \end{mytab}

\end{document}

在此处输入图片描述

相关内容