Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In a style file for thesis and dissertations in LaTeX, for my school, certain sections have green boxes. Those sections are abstract, acknowledgements, dedication, and citation. Is there a way to have words shifted according to length?

enter image description here

enter image description here

Abstract looks okay but Acknowledgements stick out on the right too much. Is it possible to have LaTeX determine based on word length how to shift the title?

In the style file, the environments are set up:

% 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}

and

% 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  
}
share|improve this question
2  
Why are you using \pgftext instead of \node? What do you mean with “stick out on the right too much”? It looks like both Abstract and Acknowledgments are pretty much the same length in the green box. And what do you want to shift about what length? Is there a reason you are using remember picture (with no coordinates/nodes) and overlay? –  Qrrbrbirlbel Dec 13 '13 at 21:44
    
You want box to start from the middle of the word?, Till what point on the right, the box and text should reach? Why not a complete MWE? –  Harish Kumar Dec 13 '13 at 23:07
    
@HarishKumar the middle would be fine. Is there a way to set this automated with added environments. –  dustin Dec 14 '13 at 0:42
    
@Qrrbrbirlbel this isn't my code. It is a style file for preparing a thesis but I don't like the look. By right, I meant to say left. –  dustin Dec 14 '13 at 0:44
    
How long the box would stretch to the right? –  Harish Kumar Dec 14 '13 at 0:51
show 2 more comments

1 Answer 1

up vote 4 down vote accepted

Your question or submission doesn't really have clear specification.

The green box starts at the middle of the text, and its length is the half of the text (with the addition of the length named \rightpadding).

enter image description here

\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}
share|improve this answer
    
Can you right align them and make the green boxes the same length? The only piece being adjusted should be word placement. –  dustin Dec 14 '13 at 16:21
    
@dustin just add a \hfill before the \begin{tikzpicture} (and I have removed the [overlay] modifiers, because they weren't needed) –  masu Dec 14 '13 at 17:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.