TikZ 框中的单词智能移动

TikZ 框中的单词智能移动

在我学校的 LaTeX 论文和学位论文样式文件中,某些部分带有绿色方框。这些部分是摘要、致谢、献词和引文。有没有办法根据长度移动单词?

在此处输入图片描述

在此处输入图片描述

摘要看起来还行,但致谢在右侧太突出了。是否可以让 LaTeX 根据字数决定如何移动标题?

在样式文件中,设置了环境:

% abstract environment                                                            
\renewenvironment{abstract}{                                                      
  \begin{tikzpicture}[remember picture, overlay]%                                 
    \pgftext[right, x = 12cm, y = 0.2cm]{                                         
      \color{uvmgreen}\Huge\bfseries Abstract};%                                  
    \draw[fill = uvmgreen, draw = uvmgreen] (15.5,-.75) rectangle (10,1);%        
    \clip (15.5,-.75) rectangle (10,1);                                           
    \pgftext[right, x = 12cm, y = 0.2cm]{                                         
      \color{white}\Huge\bfseries Abstract};%                                     
\end{tikzpicture}%                                                                

\vspace{40pt}                                                                     
}{\newpage}

% acknowledgements environment                                                    
\newenvironment{acknowledgements}{                                                
  \thispagestyle{plain}                                                           
  \begin{tikzpicture}[remember picture, overlay]%                                 
    \pgftext[right, x = 12cm, y = 0.2cm]{                                         
      \color{uvmgreen}\Huge\bfseries Acknowled\smash{g}ements};%                  
    \draw[fill = uvmgreen, draw = uvmgreen] (15.5,-.75) rectangle (10,1);%        
    \clip (15.5,-.75) rectangle (10,1);                                           
    \pgftext[right, x = 12cm, y = 0.2cm]{                                         
      \color{white}\Huge\bfseries Acknowled\smash{g}ements};%                     
  \end{tikzpicture}%                                                              

  \vspace{40pt}                                                                   
  \addcontentsline{toc}{section}{Acknowledgements}                                
  \begin{doublespace}}{\end{doublespace}                                          
  \newpage  
}

答案1

您的问题或提交确实没有明确的说明。

绿色框从文本中间开始,其长度是文本的一半(加上名为的长度\rightpadding)。

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usepackage{setspace}
\usepackage{varwidth}
%\usepackage{showframe}

\definecolor{darkgreen}{RGB}{10,90,10}

\newbox{\mybox}
\newlength{\leftwidth}
\newlength{\rightwidth}
\newlength{\width}

\newlength{\rightpadding}
\setlength{\rightpadding}{1cm}

\newcommand\greenblock[1]{%
\tikzstyle{every node}=[minimum height=30pt, inner sep=0pt, font=\Large\bfseries]%
\begin{lrbox}{\mybox}\begin{varwidth}{\linewidth}%
    \begin{tikzpicture}\node {#1};\end{tikzpicture}%
\end{varwidth}\end{lrbox}%
\setlength{\leftwidth}{\the\wd\mybox}%
\setlength{\leftwidth}{0.5\leftwidth}%
\setlength{\rightwidth}{\leftwidth}%
\addtolength{\rightwidth}{\rightpadding}%
\setlength{\width}{\leftwidth}%
\addtolength{\width}{\rightwidth}%
\begin{tikzpicture}
    \begin{scope}
        \clip[minimum width=\rightwidth] (\leftwidth,-30pt) rectangle (\width,0pt);
        \fill[color=darkgreen] (\leftwidth,-30pt) rectangle (\width,0pt)
            node[color=white] at (\leftwidth,-15pt) {#1};
    \end{scope}
    \begin{scope}
        \clip[minimum width=\leftwidth] (0,-30pt) rectangle (\leftwidth,0pt);
        \draw[draw=none] (0,-30pt) rectangle (\the\wd\mybox,0pt)
            node[color=darkgreen] at (\leftwidth,-15pt) {#1};
    \end{scope}
\end{tikzpicture}%
\vspace{30pt}%
}

\begin{document}

\noindent
\greenblock{Abstract}

\noindent
\greenblock{Acknowledgement}

\end{document}

相关内容