如何书写而不影响下一个/上一个元素的垂直位置?

如何书写而不影响下一个/上一个元素的垂直位置?

我想在一些框旁边写文字和画符号,但不影响框的所有位置,如下图所示。

在此处输入图片描述

我尝试了以下代码但它确实影响了盒子的位置。

我该如何修复它?

\documentclass{article}

\usepackage{tcolorbox}
\usepackage{showframe}
\usepackage{harmony}

\newtcolorbox{myFrame}
{colback = red, colframe = white, top = 0mm, bottom = 0mm, boxrule = 0pt, left = 2mm, right = 2mm}

\begin{document}

\vbox to0cm
{\hspace{-2cm}\hbox to2cm{\Acht\Acht} \par
\hspace{-2cm}\hbox to2cm{\small{}easy}}
\begin{myFrame}
Exercice 2.3\par
This is the text of the exercise.\par
By the way, I use tcolorbox to make my boxes.
\end{myFrame}

\vbox to0cm
{\hspace{-2cm}\hbox to2cm{\Acht} \par
\hspace{-2cm}\hbox to2cm{\small{}medium}}
\begin{myFrame}
Exercice 2.4\par
This is the text of the exercise.\par
By the way, I use tcolorbox to make my boxes.
\end{myFrame}

{\medskip\large\centering \bf With the symbols and texts in the margin\par}

%% Without the symbols+text in the margin
%\begin{myFrame}
%Exercice 2.3\par
%This is the text of the exercise.\par
%By the way, I use tcolorbox to make my boxes.
%\end{myFrame}
%
%\begin{myFrame}
%Exercice 2.4\par
%This is the text of the exercise.\par
%By the way, I use tcolorbox to make my boxes.
%\end{myFrame}
%
%{\medskip\large\centering \bf Without the symbols and texts in the margin\par}

\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

这是您想要的样子吗?我不确定框内符号的对齐方式,但\raggedleft如果需要,您可以轻松地进行对齐。

在此处输入图片描述

\documentclass{article}

\usepackage{tcolorbox}
\usepackage{showframe}
\usepackage{harmony}

\newtcolorbox{myFrame}
{colback = red, colframe = white, top = 0mm, bottom = 0mm, boxrule = 0pt, left = 2mm, right = 2mm}

\newcommand{\easy}{%
    \llap{\parbox[t][0pt]{1.7cm}{%
            \Acht\Acht\par
            \small{}easy
    }}%
}
\newcommand{\medium}{%
    \llap{\parbox[t][0pt]{1.7cm}{%
            \Acht\par
            \small{}medium
    }}%
}

\begin{document}
    
    \begin{myFrame}
        \easy
        Exercice 2.3\par
        This is the text of the exercise.\par
        By the way, I use tcolorbox to make my boxes.
    \end{myFrame}
    

    \begin{myFrame}
        \medium
        Exercice 2.4\par
        This is the text of the exercise.\par
        By the way, I use tcolorbox to make my boxes.
    \end{myFrame}
    
    {\medskip\large\centering \bf With the symbols and texts in the margin\par}

\end{document}

答案2

不可否认的是,它并不完美,因为缺少圆角并且框不会自动编号,但以下内容可以作为起点:

在此处输入图片描述

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{showframe}
\usepackage{harmony}

\usepackage{calc}
\usepackage{changepage}

\newlength{\myleftcolwidth}
\setlength{\myleftcolwidth}{\widthof{\small medium}}
\newtcolorbox{mybox}{sidebyside,
                     sidebyside align=top seam,
                     boxrule=0pt,
                     boxsep=3pt, 
                     sharp corners,
                     lefthand width=\myleftcolwidth, 
                     lower separated=true, 
                     sidebyside gap=3mm,
                     bicolor,
                     colback=white,
                     colbacklower=red, 
                     left=3pt}
                      
\newenvironment{myexercise}[2]%
  {%
    \begin{adjustwidth*}{-\myleftcolwidth-3mm-2.5pt}{}%
    \begin{mybox}%
    \hfill\begin{tabular}[t]{@{}r@{}}#1 \\\small #2 \end{tabular}%
    \tcblower%
  }%
  {%
    \end{mybox}%
    \end{adjustwidth*}%
  }

\usepackage{lipsum}

\begin{document}

\begin{myexercise}{\Acht\Acht}{easy}
\lipsum[5]
\end{myexercise}

\begin{myexercise}{\Acht}{medium}
\lipsum[1][1-4]
\end{myexercise}

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

\end{document}

答案3

这是一个自动对框进行编号并在左边距添加注释的解决方案overlay。注释内容是必填参数,可以留空。

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{showframe}
\usepackage{harmony}

\newtcolorbox[auto counter, number within=section]{myFrame}[2][]
{enhanced, attach title to upper=\\, title=Exercice~\thetcbcounter, coltitle=black, colback = purple!60, colframe = white, top = 0mm, bottom = 0mm, boxrule = 0pt, left = 2mm, right = 2mm,
overlay={\node[anchor=north east, align=right] at (frame.north west) {#2};},#1}

\begin{document}

\section{one section}

\section{another section}

\begin{myFrame}{\Acht\Acht\\{\small easy}}
This is the text of the exercise.\par
By the way, I use tcolorbox to make my boxes.
\end{myFrame}

\begin{myFrame}{\Acht\Acht\\{\small medium}}
This is the text of the exercise.\par
By the way, I use tcolorbox to make my boxes.
\end{myFrame}

{\medskip\large\centering \bf With the symbols and texts in the margin\par}

% Without the symbols+text in the margin
\begin{myFrame}{}
This is the text of the exercise.\par
By the way, I use tcolorbox to make my boxes.
\end{myFrame}

\begin{myFrame}{}
This is the text of the exercise.\par
By the way, I use tcolorbox to make my boxes.
\end{myFrame}

{\medskip\large\centering \bf Without the symbols and texts in the margin\par}

\end{document}

在此处输入图片描述

相关内容