GeneralPlacing argument variables into other function arguments

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
4fingers
Posts: 12
Joined: Fri Jul 03, 2009 11:57 am

Placing argument variables into other function arguments

Post by 4fingers »

Hi,

I am currently working on the Tikz package using the example found here:
http://www.texample.net/tikz/examples/b ... -and-math/

Where the following example works perfectly:

Code: Select all

\documentclass{article}
\usepackage{lipsum}   % To generate test text 
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}
\begin{document}

% Define box and box title style
\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} =[fill=red, text=white]

\begin{tikzpicture}
\node [mybox] (box){%
    \begin{minipage}{0.50\textwidth}
        \lipsum[11]     
    \end{minipage}
};
\node[fancytitle, right=10pt] at (box.north west) {A fancy title};
\end{tikzpicture}
\end{document}
However I need to make it more modular, so the following now converts it to a command:

Code: Select all

\documentclass{article}
\usepackage{lipsum}   % To generate test text 
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}
\begin{document}

% Define Generic Box Styles
\definecolor{Generic_border}{HTML}{005000}
\definecolor{Generic_background}{HTML}{ccdecc}
\tikzstyle{Generic_mybox} = [draw=Generic_border, fill=Generic_background, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{Generic_fancytitle} =[draw=Generic_border, fill=Generic_background, rounded corners]

% Define Summary Box Styles
\definecolor{Summary_border}{HTML}{AA3F3C}
\definecolor{Summary_background}{HTML}{EECFCE}
\tikzstyle{Summary_mybox} = [draw=Summary_border, fill=Summary_background, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{Summary_fancytitle} =[draw=Summary_border, fill=Summary_background, rounded corners]

% Create Info Box command
\newcommand{\infobox}[2]{
	\begin{tikzpicture}
		\node [Generic_mybox] (box){%
			\begin{minipage}{0.50\textwidth}
				  #2
			\end{minipage}
		};
		\node[Generic_fancytitle, right=10pt] at (box.north west) {#1};
	\end{tikzpicture}
}

\infobox{Title}{\lipsum[11]} 
\infobox{Title}{\lipsum[11]}

\end{document}
The final stage is to parametrise the command further so I can easily choose which type of box I would like:

Code: Select all

\documentclass{article}
\usepackage{lipsum}   % To generate test text 
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}
\begin{document}

% Define Generic Box Styles
\definecolor{Generic_border}{HTML}{005000}
\definecolor{Generic_background}{HTML}{ccdecc}
\tikzstyle{Generic_mybox} = [draw=Generic_border, fill=Generic_background, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{Generic_fancytitle} =[draw=Generic_border, fill=Generic_background, rounded corners]

% Define Summary Box Styles
\definecolor{Summary_border}{HTML}{AA3F3C}
\definecolor{Summary_background}{HTML}{EECFCE}
\tikzstyle{Summary_mybox} = [draw=Summary_border, fill=Summary_background, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{Summary_fancytitle} =[draw=Summary_border, fill=Summary_background, rounded corners]

% Create Info Box command
\newcommand{\infobox}[4]{
	\begin{tikzpicture}
		\node [#2] (box){%
			\begin{minipage}{0.50\textwidth}
				  #2
			\end{minipage}
		};
		\node[#3, right=10pt] at (box.north west) {#1};
	\end{tikzpicture}
}

\infobox{Title, Generic_mybox, Generic_fancytitle}{\lipsum[11]} 
\infobox{Title, Summary_mybox, Summary_fancytitle}{\lipsum[11]}

\end{document}
However the above example fails as it doesn't like me placing argument variables as function arguments.

Any idea how I can get round this issue,
Thanks

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Placing argument variables into other function arguments

Post by cgnieder »

You have defined a macros with 4 arguments but are using it with only 2.

I would probably use \tikzset to define the styles (and use the decorations library instead of the snakes library which is deprecated, but it isn't needed here anyway). After adding a few % and reducing the minipage to fit in the half of the page everything works:

Code: Select all

\documentclass{article}
\usepackage{showframe}% to visualize page dimensions
\usepackage{lipsum}   % To generate test text
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}

% Define Generic Box Styles
\definecolor{Generic_border}{HTML}{005000}
\definecolor{Generic_background}{HTML}{ccdecc}
\tikzset{
  Generic_mybox/.style={
    draw=Generic_border,
    fill=Generic_background,
    very thick,
    rectangle,
    rounded corners,
    inner sep=10pt,
    inner ysep=20pt},
  Generic_fancytitle/.style={
    draw=Generic_border,
    fill=Generic_background,
    rounded corners}
}

% Define Summary Box Styles
\definecolor{Summary_border}{HTML}{AA3F3C}
\definecolor{Summary_background}{HTML}{EECFCE}
\tikzset{
  Summary_mybox/.style={
    draw=Summary_border,
    fill=Summary_background,
    very thick,
    rectangle,
    rounded corners,
    inner sep=10pt,
    inner ysep=20pt},
  Summary_fancytitle/.style={
    draw=Summary_border,
    fill=Summary_background,
    rounded corners}
}

% Create Info Box command
\newcommand{\infobox}[4]{%
  \begin{tikzpicture}
    \node [#2] (box){%
      \begin{minipage}{\dimexpr 0.50\linewidth-23pt}
       #4
      \end{minipage}%
    };
    \node[#3, right=10pt] at (box.north west) {#1};
  \end{tikzpicture}%
}

\noindent
\infobox{Title}{Generic_mybox}{Generic_fancytitle}{\lipsum[11]}
\infobox{Title}{Summary_mybox}{Summary_fancytitle}{\lipsum[11]}

\end{document}
Regards
site moderator & package author
4fingers
Posts: 12
Joined: Fri Jul 03, 2009 11:57 am

Placing argument variables into other function arguments

Post by 4fingers »

Thanks for spotting my silly mistake and tidying up the libraries and styles. I have taken it further by removing the need of a parameter and reduced the duplication by placing the \tikzset command into the \newcommand:

Code: Select all

\documentclass{article}
\usepackage{showframe}% to visualize page dimensions
\usepackage{lipsum}   % To generate test text
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}

% Define Generic Box Styles
\definecolor{GenericBorder}{HTML}{005000}
\definecolor{GenericBackground}{HTML}{CCDECC}

% Define Summary Box Styles
\definecolor{SummaryBorder}{HTML}{AA3F3C}
\definecolor{SummaryBackground}{HTML}{EECFCE}

% Create Info Box command
\newcommand{\infobox}[3]{%
	\tikzset{
		mybox/.style={
			draw=#2Border,
			fill=#2Background,
			very thick,
			rectangle,
			rounded corners,
			inner sep=10pt,
			inner ysep=20pt},
		fancytitle/.style={
			draw=#2Border,
			fill=#2Background,
			rounded corners}
	}
  
	\begin{tikzpicture}
		\node [mybox] (box){%	
			\begin{minipage}{\dimexpr 0.50\linewidth-23pt}
				#3
			\end{minipage}%
		};
		\node[fancytitle, right=10pt] at (box.north west) {#1};
	\end{tikzpicture}%
}

\noindent
\infobox{Title}{Generic}{\lipsum[11]}
\infobox{Title}{Summary}{\lipsum[11]}

\end{document}
The final thing I am having trouble with is centring the box. I have tried placing it in a centred box but that only centres the text within it.

Thanks
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Placing argument variables into other function arguments

Post by cgnieder »

Something like this?

Code: Select all

\documentclass{article}
\usepackage{showframe}% to visualize page dimensions
\usepackage{lipsum}   % To generate test text
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}

% Define Generic Box Styles
\definecolor{GenericBorder}{HTML}{005000}
\definecolor{GenericBackground}{HTML}{CCDECC}

% Define Summary Box Styles
\definecolor{SummaryBorder}{HTML}{AA3F3C}
\definecolor{SummaryBackground}{HTML}{EECFCE}

% Create Info Box command
\newcommand{\infobox}[3]{%
  \tikzset{
    mybox/.style={
    draw=#2Border,
    fill=#2Background,
    very thick,
    rectangle,
    rounded corners,
    inner sep=10pt,
    inner ysep=20pt},
    fancytitle/.style={
    draw=#2Border,
    fill=#2Background,
    rounded corners}
  }%
  \centerline{%
  \begin{tikzpicture}
    \node [mybox] (box){%  
    \begin{minipage}{0.50\linewidth}
      #3
    \end{minipage}%
  };
  \node[fancytitle, right=10pt] at (box.north west) {#1};
  \end{tikzpicture}%
  }%
}

\noindent
\infobox{Title}{Generic}{\lipsum[11]}
\infobox{Title}{Summary}{\lipsum[11]}

\end{document}
Regards
site moderator & package author
4fingers
Posts: 12
Joined: Fri Jul 03, 2009 11:57 am

Re: Placing argument variables into other function arguments

Post by 4fingers »

Well that sets the box width to half the page but doesn't centre the box horizontally within the page width.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Placing argument variables into other function arguments

Post by cgnieder »

Are you using a different class with different margins or something? When I compile the code I get boxes that are centered both with respect to the textwidth and the paperwidth!
site moderator & package author
4fingers
Posts: 12
Joined: Fri Jul 03, 2009 11:57 am

Re: Placing argument variables into other function arguments

Post by 4fingers »

Sorry, problem existed between keyboard and chair, everything works as expected.

Thanks again for all your help :)
Post Reply