# Detect actual start of text

I have conditional which is sometimes suppressed, and if it gets typeset needs to be noted.

If the conditional text begins with an environment which adds vertical space prior to the actual start of the text, then the \tikzmark location needs tweaking. Note how in paragraph mode, the text is appropriately marked, but when starting with an align*, or enumerate the brace drawn is too large:

So, what I need is to be able to detect the location of the start of the text and the end of the text, which does not include any leading or trailing space.

## Notes:

• Moving the \MarkThisText to be within an align* or enumerate won't work for me as the entire block may be suppressed, in which case I need to suppress the begin, end of the environment which would be difficult if the \MarkThisText didn't start until after \begin{align*}.
• I thought about setting a flag with an align and enumerate type environments, but that would fail if the align or enumerate happens to be embedded in the middle of \MarkThisText.

## Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathreplacing}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node[baseline] (#1) {};}

\newcommand{\DrawBrace}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\coordinate (Top) at
($(#2.north -| current page.south east)+(-4.5cm,0.25\baselineskip)$);
\coordinate (Bottom) at
($(#3.south -| current page.south east)+(-4.5,-0.25\baselineskip)$);
\draw [
line width=1.0pt,
decoration={brace, amplitude=0.30em},
decorate, #1,
] (Top) -- (Bottom)
node[rotate=90, midway, below] {Note};
\end{tikzpicture}%
}

\newcommand{\MarkThisText}[2][]{%
%% conditions to determine if this text is to be suppressed omitted
%% \iftoggle{SuppressThisText}{}{%
\tikzmark{Start}%
\color{#1}%
#2%
\color{black}%
\tikzmark{End}%
\DrawBrace[#1]{Start}{End}%
%% } % end \iftoggle{DisplayThisText}
}%

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at dignissim eros.
Morbi faucibus sit amet felis a accumsan.  Aliquam
dapibus pellentesque dui, vitae aliquam turpis semper eget.

\MarkThisText[blue]{%
Donec egestas leo nibh, at malesuada leo congue non.
Cras laoreet lectus sed tortor laoreet, ac aliquet nibh condimentum.
\begin{align*}
F &= ma \\
\implies E &= mc^2
\end{align*}
Nam elementum ante non nunc facilisis porttitor. Nullam non suscipit lorem.
Sed arcu dui, accumsan et pretium dictum, venenatis id metus.%
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at dignissim eros.
Morbi faucibus sit amet felis a accumsan.  Aliquam
dapibus pellentesque dui, vitae aliquam turpis semper eget.

Text before \verb|align| should \emph{not} be noted":
\MarkThisText[red]{%
\begin{align*}
F &= ma \\
\implies E &= mc^2
\end{align*}
}%
Text after \verb|align| should \emph{not} be noted".

Text before \verb|enumerate| should \emph{not} be noted":
\MarkThisText[brown]{%
\begin{enumerate}
\item List Item 1
\item List Item 2
\end{enumerate}
}%
Text after \verb|enumerate| should \emph{not} be noted".
\end{document}


## 1 Answer

I'd position the markers by hand.

\documentclass{article}
\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathreplacing}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node[baseline] (#1) {};}

\newcommand{\DrawBrace}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\coordinate (Top) at
($(#2.north -| current page.south east)+(-4.5cm,0.25\baselineskip)$);
\coordinate (Bottom) at
($(#3.south -| current page.south east)+(-4.5,-0.25\baselineskip)$);
\draw [
line width=1.0pt,
decoration={brace, amplitude=0.30em},
decorate, #1,
] (Top) -- (Bottom)
node[rotate=90, midway, below] {Note};
\end{tikzpicture}%
}

\newcommand{\MarkThisText}[2][]{%
%% conditions to determine if this text is to be suppressed omitted
%% \iftoggle{SuppressThisText}{}{%
\color{#1}%
#2%
\color{black}%
%% } % end \iftoggle{DisplayThisText}
}%

\newcommand\startmark[1]{\tikzmark{start#1}}
\newcommand\stopmark[1]{\tikzmark{stop#1}\DrawBrace{start#1}{stop#1}}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at dignissim eros.
Morbi faucibus sit amet felis a accumsan.  Aliquam
dapibus pellentesque dui, vitae aliquam turpis semper eget.

\MarkThisText[blue]{%
\startmark{a}%
Donec egestas leo nibh, at malesuada leo congue non.
Cras laoreet lectus sed tortor laoreet, ac aliquet nibh condimentum.
\begin{align*}
F &= ma \\
\implies E &= mc^2
\end{align*}
Nam elementum ante non nunc facilisis porttitor. Nullam non suscipit lorem.
Sed arcu dui, accumsan et pretium dictum, venenatis id metus.%
\stopmark{a}%
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at dignissim eros.
Morbi faucibus sit amet felis a accumsan.  Aliquam
dapibus pellentesque dui, vitae aliquam turpis semper eget.

Text before \verb|align| should \emph{not} be noted":
\MarkThisText[red]{%
\begin{align*}
\startmark{b}F &= ma \\
\implies E &= mc^2\stopmark{b}%
\end{align*}
}%
Text after \verb|align| should \emph{not} be noted".

Text before \verb|enumerate| should \emph{not} be noted":
\MarkThisText[brown]{%
\begin{enumerate}
\item \startmark{c}List Item 1
\item List Item 2\stopmark{c}
\end{enumerate}
}%
Text after \verb

|enumerate| should \emph{not} be noted".
\end{document}

• Yes that will work for the MWE provided, but due to there also being conditional text within the block, I don't know upfront if the environments which have surrounding space are at the beginning or end of the text to be marked. – Peter Grill Jun 28 '14 at 22:33
• @PeterGrill I thought the conditional omitted the entire environment and the brace, so woul domit the tikzmarks as well? – David Carlisle Jun 28 '14 at 23:34
• @PeterGrill although I'd put the start color in the wrong place, just moved it – David Carlisle Jun 28 '14 at 23:35
• Well, yeah it is in the MWE, but in my actual use there are several layers of conditions, and of course the tikzmarks could also be added to those conditions as well, but I'd prefer to keep the logic in \MarkThisText if at all possible. Perhaps using \everypar to set a flag and then something like \AtBeginEnvironment{align}{if{NoParYet}{\tikzmark{Top}}}... Just thought of this now... – Peter Grill Jun 28 '14 at 23:58