# Smart shifting of words in a TikZ box

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?

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}
\begin{doublespace}}{\end{doublespace}
\newpage
}

-
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

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).

\documentclass{article}

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

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

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

\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}%
\setlength{\width}{\leftwidth}%
\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}

-
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