0

Firstly, My english is not good so if there is any mistake, please let me know.

I want to create a new environment in latex in order to make this frame enter image description here But when the text in frame is too long, there are some problems

enter image description here

Can you help me to solve my problems. Here is my code

\newcounter{vd}

\setcounter{vd}{0}

\newenvironment{vd}[1][]{
\def\ghichu{\textit{\color{blue}#1.}}
\addtocounter{vd}{1}
\noindent
\begin{tikzpicture}
\node[inner sep=10pt,fill=green!20] (vd)
\bgroup
\begin{minipage}{0.96\textwidth}
\textbf{\color{red}Ví dụ \thevd:}
}{
\flushright\ghichu

\end{minipage}

\egroup;

\draw[blue,line width=5pt] (vd.north west)--(vd.south west);

\end{tikzpicture}

}

Any help is appreciated

Bernard
  • 271,350
129492
  • 59

1 Answers1

2

I suggest to use a breakable tcolorbox instead of a TiKZ environment.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}

\newtcolorbox[auto counter]{vd}[1][]{
    enhanced,
    breakable,
    sharp corners,
    colback=green!20,
    colframe=blue,
    boxrule=0pt,
    leftrule=2pt,
    detach title,
    coltitle=red,
    fonttitle=\bfseries,
    title={Vi du~\thetcbcounter},
    before upper={\tcbtitle\quad}
    #1
}

\begin{document}
\begin{vd}
\lipsum[1]
\end{vd}

\begin{vd}
\lipsum[1-5]
\end{vd}
\end{document}

enter image description here

Ignasi
  • 136,588
  • I am most grateful for your support. Thank you for taking the time to help me, I really do appreciate it. – 129492 Feb 04 '20 at 13:13
  • Hi, i have a problem. I want to reset counter when i use \begin{vd} \end{vd}. Although i add \setcounter{vd}{0}, i can't do it. Can you help me? – 129492 May 14 '20 at 18:20
  • 1
    @129492 When do you want reset the counter? After a section? Then insert number within=section beside autocounter. Please take a look at section "Initialization Option Keys" in tcolorbox documentation. By the way, the counter is not vd but \tcbcounter. – Ignasi May 15 '20 at 07:15
  • I want to reset the counter in different times so i decided to add "\setcounter{...}{0}". I added \setcounter{tcbcounter}{0} but it didn't change the number – 129492 May 15 '20 at 08:27
  • i found the solution for my problems. I added "\newcounter{mycounter} \newcommand\showmycounter{\refstepcounter{mycounter}\themycounter}" and replace " title={Ví dụ~\thetcbcounter:}," to title={Ví dụ~\showmycounter:}, – 129492 May 15 '20 at 08:59