Tikz 中的条件宽度

Tikz 中的条件宽度

我正在尝试制作一组​​两个函数来格式化页面相对两侧的 tikz 标注中的引号。我想将标注的宽度限制为 3 英寸,但我的ifthenelse语句不配合!任何帮助都将不胜感激。

\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{shapes.callouts}
\usetikzlibrary{shadows}
\usepackage[letterpaper,margin=0.0in,tmargin=1.0in,bmargin=1.0in]{geometry}

\definecolor{lightblue}{RGB}{50,50,205}
\definecolor{lightgreen}{RGB}{50,205,50}

\newlength\maxWidth
\setlength\maxWidth{3in}

\pgfkeys{%
   /calloutquote/.cd,
   width/.code                   =  {\def\calloutquotewidth{#1}},
   position/.code                =  {\def\calloutquotepos{#1}}, 
   author/.code                  =  {\def\calloutquoteauthor{#1}},
   /calloutquote/.unknown/.code   =  {\let\searchname=\pgfkeyscurrentname
                                \pgfkeysalso{\searchname/.try=#1,                                
   /tikz/\searchname/.retry=#1},\pgfkeysalso{\searchname/.try=#1,
                                 /pgf/\searchname/.retry=#1}}
                           }  

\newcommand\calloutquote[2][]{%
      \pgfkeys{/calloutquote/.cd,
        width               = 3in,
        position            = {(0,-1)},
        author              = {}}
 \pgfqkeys{/calloutquote}{#1}                   
 \node [drop shadow={opacity=0.25}, rectangle callout,callout relative pointer={\calloutquotepos},text width=\calloutquotewidth,/calloutquote/.cd,
    #1] (tmpcall) at (0,0) {#2};
 \node at (tmpcall.pointer){\calloutquoteauthor};
 }

 \newcommand\quoteLeft[1]{%
    \def\length{\widthof{#1}}
    \ifthenelse{\lengthtest{\length>\maxWidth}}{\setlength\length{\maxWidth}}{}
    \hspace{0.5in}\calloutquote[width=\length,fill=lightgreen!30,rounded corners]{#1}
}

 \newcommand\quoteRight[1]{%
    \def\length{\widthof{#1}}
    \hspace{6.5in-\length}\calloutquote[width=\length,position{(1,-0.5)},fill=lightblue!30,rounded corners]{#1}
}

\begin{document}
\begin{tikzpicture}

\end{tikzpicture} 
    \quoteLeft{Statement 1?}   
\begin{tikzpicture}
    \quoteRight{Statement 2!}
\end{tikzpicture} 
\end{document}

答案1

以下是一个宏的示例,它返回给定文本的长度,最大长度为\maxWidth(本例中为50pt),应该适合您的情况。传入的文本是,\hspace*{}这样我就可以知道比较是否有效:

在此处输入图片描述

代码:

\documentclass{article}

\newlength\maxWidth
\setlength\maxWidth{50pt}


\usepackage{printlen}

\newcommand*{\SetMinWidth}[2]{%
    % #1 = length to take on the minimum dimension
    % #2 = text who's width is to measure
    \settowidth{#1}{#2}%
    \ifdim#1>\maxWidth\relax%
        \setlength{#1}{\maxWidth}%
    \fi%
}%


\newlength{\WidthToUse}

\begin{document}
    \SetMinWidth{\WidthToUse}{\hspace*{10pt}}
    \printlength{\WidthToUse}


    \SetMinWidth{\WidthToUse}{\hspace*{50pt}}
    \printlength{\WidthToUse}


    \SetMinWidth{\WidthToUse}{\hspace*{70pt}}
    \printlength{\WidthToUse}
\end{document}

答案2

我停止使用ifthenelse包,它工作得很好。我很想知道如何更好地格式化页面上生成的 TikZ 节点,但这是另一个问题!

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{shapes.callouts}
\usetikzlibrary{shadows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usepackage[letterpaper,margin=0.5in,tmargin=0.75in,bmargin=1.00in]{geometry}
\usepackage{pbox}

\newcommand*{\SetMaxWidth}[3]{
    \settowidth{#1}{\normalsize#2}
    \settowidth{\timestampWidth}{\footnotesize#3}
    \ifdim#1<\timestampWidth
        \setlength{#1}{\timestampWidth}
    \fi
    \ifdim#1>\maxWidth
        \setlength{#1}{\maxWidth}
    \fi
}

\newlength\widthToUse

\newcommand\quoteLeft[2]{%
    \SetMaxWidth{\widthToUse}{#1}{#2}
    \setbox0=\vbox{\parbox[b]{1.1\widthToUse}{#1}}
    \tikz[remember picture]{
        \node[anchor=north west,overlay,rectangle,
                rounded corners,drop shadow={opacity=0.5},
                fill=leftcolor,text width=\widthToUse]
            at (0,0) {\footnotesize#2 \\ \vspace{8pt} \normalsize#1};}
    \vspace{1.25\ht0}
    \vspace{\spaceAfter}
}

\newcommand\quoteRight[2]{%
    \SetMaxWidth{\widthToUse}{#1}{#2}
    \hspace{\rightSpace}\hspace{-\widthToUse}
    \setbox0=\vbox{\parbox[b]{1.1\widthToUse}{#1}}
    \tikz[remember picture]{
        \node[anchor=north west,overlay,rectangle,
                rounded corners,drop shadow={opacity=0.5},
                fill=rightcolor,text width=\widthToUse]
            at (0,0) {\footnotesize#2 \\ \vspace{8pt} \normalsize#1};}
    \vspace{1.25\ht0}
    \vspace{\spaceAfter}
}

\begin{document}
\quoteRight{
Hey!
}{14 Jan 2014}

\quoteLeft{
 How are you?
}{14 Jan 2014}
\end{document}

相关内容