创建环境

创建环境

我正在尝试创建一个环境。下面的代码执行的操作与我想要的类似。我的困难在于调整将保留在环境中的内容。我想减少文本的宽度,并让侧线伴随整个环境,即使环境占据了不止一页。

\documentclass[a4paper,12pt]{book}
\usepackage{lipsum}
\usepackage[top=2cm,bottom=3cm,left=2cm,right=2cm]{geometry}
\usepackage[brazil]{babel} 
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc} 
\usepackage{tikz}
\usetikzlibrary{calc}

%%%%%%%%%%%%%%% ENVIROMENT  %%%%%%%%%%%%

\newcounter{mycounter}
\setcounter{mycounter}{1}
\newenvironment{my}
{%
\noindent \begin{tikzpicture}
\node[fill=black, text=white, inner sep=6pt] (a) {\textbf{{\large Exemplo \themycounter}}};
\draw[line width=2pt,color=black](a.north west)--($(a.north west)+(0,-20)$);
\end{tikzpicture}\vspace{-19cm}
}
{}

\begin{document}
\begin{my}
\lipsum[1-20]
\end{my}
\end{document}

答案1

我认为你不需要大锤子。以下是基于环境的提议leftbar,来自framed软件包:

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage[top=2cm,bottom=3cm, hmargin=2cm]{geometry}
\usepackage{framed}
%%%%%%%%%%%%%%% ENVIRONMENT %%%%%%%%%%%%

\newcounter{mycounter}

\newenvironment{mine}%
{\setlength{\fboxsep}{6pt}\refstepcounter{mycounter}\leftbar\hspace*{-\dimexpr\parindent+15pt} \colorbox{black}{\color{white}\bfseries\sffamily Ejemplo \themycounter}\smallskip\par\noindent}%
{\endleftbar}

\begin{document}

\begin{mine}
\lipsum[1-12]
\end{mine}

\end{document} 

在此处输入图片描述

答案2

另一种解决方案是tcolorbox

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

\newtcolorbox[auto counter]{mine}[2][]{
    enhanced, breakable,
    title={Ejemplo~\thetcbcounter: #2}, 
    colbacktitle=black, coltitle=white, fonttitle=\bfseries,
    frame hidden, colback=white,
    attach boxed title to top left,
    boxed title style={sharp corners, size=fbox},
    sharp corners,
    left=3mm, boxsep=0pt, right=0pt, bottom=0pt,
    borderline west={2pt}{0pt}{black},
}

\begin{document}

\lipsum[1]

\begin{mine}{}
\lipsum[1-4]
\end{mine}

\begin{mine}{Very important}
\lipsum[2]
\end{mine}

\begin{mine}{Very very important}
\lipsum[2]
\end{mine}

\end{document}

在此处输入图片描述

相关内容