创建在文本旁边绘制规则的环境

创建在文本旁边绘制规则的环境

在我的文档中,我经常会提出一个问题,然后我会通过加粗文本来标记。我想在文本的左侧或右侧另外放置一个 2-3 毫米的灰色规则,如下例所示:

在此处输入图片描述

是否可以将其变成一个环境,以便我只需写入\question{....}?目前我还没有决定是否要将此规则放在问题的左侧或右侧,但想看看如何做到这一点。

这是我目前的 MWE,其中包含规则之外的所有内容:

\documentclass[a5paper, 14pt]{memoir}

\usepackage{lipsum}
\begin{document}
\chapter{One}
\textbf{This is a question?}

And here is the answer. And here is the answer. And here is the answer. And here is the answer. And here is the answer. 

\end{document}

答案1

我建议使用该tcolorbox包并应用一个\newtcbox绘制带有borderline west一些指定颜色的文本(在语言中为“西” TikZ)。

如果规则应该出现在右侧,则使用borderline east。第一个3mm表示规则的宽度,第二个-3mm表示向左侧移动。

\documentclass[a5paper, 14pt]{memoir}

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

\newtcbox{questionbox}[1][]{%
  enhanced,
  frame hidden,
  colback=white,
  fontupper=\bfseries,
  left=0pt,
  nobeforeafter,
  top=0pt,
  bottom=0pt,
  borderline west={3mm}{-3mm}{gray},
  #1
}

\begin{document}
\chapter{One}
\questionbox{This is a question?}

And here is the answer. And here is the answer. And here is the answer. And here is the answer. And here is the answer. 

\end{document}

在此处输入图片描述

更新

使用tcolorbox环境来允许换行和breakable分页。

\documentclass[a5paper, 14pt]{memoir}

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

\usepackage{blindtext}

\newtcolorbox{questionbox}[1][]{%
  enhanced,
  frame hidden,
  colback=white,
  fontupper=\bfseries,
  left=0pt,
  nobeforeafter,
  top=0pt,
  bottom=0pt,
  borderline west={3mm}{-3mm}{gray},
  breakable,
  #1
}



\begin{document}
\chapter{One}
\begin{questionbox}
  This is a question \blindtext?
\end{questionbox}

And here is the answer. And here is the answer. And here is the answer. And here is the answer. And here is the answer. 

\end{document}

在此处输入图片描述

答案2

仅使用包的简单命令和基于的环境的xcolor环境,可以跨页面中断:leftbarframed

\documentclass[a5paper, 14pt]{memoir}

\usepackage{lipsum}
\usepackage{framed} 
\usepackage[svgnames]{xcolor}
\newcommand\myquestion[1]{\par\noindent\llap{\color{Gainsboro}\rule[-0.25ex]{0.5em}{2ex}\hspace{\marginparsep}}\textbf{#1}\par}

\newenvironment{Question}{\par\noindent%
\def\FrameCommand{\hspace{-1.1em}{\color{Gainsboro}\vrule width 0.5em}\hspace{0.6em}}%
\MakeFramed {\advance\hsize-\width \FrameRestore}\bfseries\strut\hspace{-1.36\parindent}}%
{\endMakeFramed\vspace{-\topsep}}
\begin{document}

\chapter{One}

\myquestion{This is a question?}

And here is the answer. And here is the answer. And here is the answer. And here is the answer. And here is the answer. 

\begin{Question}
This is a very long question?
Is this indeed a very long question?
\end{Question}

And here is a short answer.

\end{document} 

在此处输入图片描述

相关内容