如何使任意内容居中于两条水平线之间?

如何使任意内容居中于两条水平线之间?

我想要实现一个只接受一个参数的宏,它将在内容上方和下方创建一行并忽略文本缩进。

这意味着无论我现在处于什么环境中(例如在枚举或逐项列举中),都将一些事件信息放置在协议中。

它应该中断当前块并始终使用完整的列宽,然后再继续该块。

但是,我在垂直居中方面遇到了问题:

\newcommand{\bartext}[1]{
    \kern4pt % space above the rules
    \hrule height 0.5pt
    \vspace{0.1cm}
    \noindent #1
    \vspace{0.1cm}
    \hrule height 0.5pt
    \kern4pt % space below the rules
}

% Example Usage:

\begin{itemize}
   \item First Point
   \item Second Point
   \bartext{Charlie enters the meeting}
   \item third point
   \fouth point
\end{itemize}

文本“查理进入会议”应从左侧开始,就像“之前的一些文本”一样。

而且线条的位置很奇怪,并且线条内的文本没有垂直居中。

有什么线索/建议吗?

例子

答案1

您可以使用(非浮动)浮点数:

\documentclass{article}
\usepackage{array,float}
\newcommand{\bartext}[1]{%
\par\medskip
\begingroup
\intextsep=\smallskipamount
\extrarowheight =3pt %or perhaps a bit less ...
\arrayrulewidth=.5pt
 \begin{figure}[H]%
 \begin{tabular}{@{}p{\textwidth}@{}}
 \hline
 #1
 \\\hline
 \end{tabular}%
 \end{figure}%
\par\endgroup}

\begin{document}
a
   \bartext{Charlie enters the meeting}
b
\begin{itemize}
   \item First Point
   \item Second Point
   \bartext{Charlie enters the meeting}
   \item third point
   \item fouth point
\end{itemize}
\end{document}

在此处输入图片描述

答案2

假设你不想使用类似

\end{itemize}
\bartext{...}
\begin{itemize}[resume*]

你可以使用以下代码,其灵感来自我如何检查当前代码是否在某个环境中?

\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum}


\makeatletter
\def\itemizename{itemize}
\def\enumeratename{enumerate}
\newcommand{\bartext}[1]{%
  \ifx\@currenvir\itemizename
    \end{itemize}
    \kern4pt % space above the rules
    \hrule height 0.5pt
    \vspace{0.1cm}
    \noindent #1
    \vspace{0.1cm}
    \hrule height 0.5pt
    \kern4pt
    \begin{itemize}[resume*]
  \else
    \ifx\@currenvir\enumeratename
    \end{enumerate}
    \kern4pt % space above the rules
    \hrule height 0.5pt
    \vspace{0.1cm}
    \noindent #1
    \vspace{0.1cm}
    \hrule height 0.5pt
    \kern4pt
    \begin{enumerate}[resume*]
  \else
    \kern4pt % space above the rules
    \hrule height 0.5pt
    \vspace{0.1cm}
    \noindent #1
    \vspace{0.1cm}
    \hrule height 0.5pt
    \kern4pt
  \fi
  \fi}
\newcommand\@myenvname{myenv}
\makeatother

\begin{document}

\lipsum[5]

\begin{itemize}
   \item First Point
   \item Second Point
   \bartext{Charlie enters the meeting}   
   \item third point
   \item fouth point
\end{itemize}

\lipsum[5]

\begin{enumerate}
   \item First Point
   \item Second Point
   \bartext{Charlie enters the meeting}   
   \item third point
   \item fouth point
\end{enumerate}

\lipsum[5]

\bartext{Charlie enters the meeting}  

\end{document}

在此处输入图片描述

答案3

您可以使用以下功能enumitem

\documentclass{report}%{memoir}
\usepackage{enumitem}
\usepackage{lipsum}

\newcommand{\bartext}[1]{
\kern4pt % space above the rules
\hrule height 0.5pt
\vspace{0.1cm}
\noindent #1
\vspace{0.1cm}
\hrule height 0.5pt
\kern4pt % space below the rules
}

\begin{document}

Some text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text.

\begin{itemize}[after=\vspace*{-\topsep}]
   \item First Point
   \item Second Point
\end{itemize}
   \bartext{Charlie enters the meeting}
\begin{itemize}[resume*, before=\vspace*{-\topsep}]
   \item third point
   \item fourth point
\end{itemize}

\end{document} 

在此处输入图片描述

相关内容