1

I generated a simple block diagram using the following code:

\documentclass[12pt,letterpaper]{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}

\tikzstyle{block} = [draw, rectangle, 
minimum height=3em, minimum width=5em, align=left]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]

% The block diagram code is probably more verbose than necessary
\begin{tikzpicture}[auto,>=latex']
    % We start by placing the blocks
    \node [block, name=message-source] {message \\ source};
    \node [block, name=encoder, right of=message-source, node distance=3.5cm] {encoder};
    \node [block, name=channel, right of=encoder, node distance=3.5cm, pin={[pinstyle]above:\scriptsize noise},
    node distance=3.5cm] {channel};
    \node [block, name=decoder, right of=channel, node distance=3.5cm] {decoder};
    \node [block, name=user, right of=decoder, node distance=3.5cm] {user};


    % Once the nodes are placed, connecting them is easy. 
    \draw [->] (message-source) -- node[name=message, align=center] {\scriptsize message} (encoder);
    \draw [->] (encoder) -- node[name=codeword, align=center] {\scriptsize codeword} (channel);
    \draw [->] (channel) -- node[name=receivedword, align=center, text width=1.75cm] {{\scriptsize received word}} (decoder);
    \draw [->] (decoder) -- node[name=decodedmsg, align=center, text width=1.75cm] {{\scriptsize decoded message}} (user);
\end{tikzpicture}

\end{document}

enter image description here

I want to draw your attention to how "received word" and "decoded message" are split with "too much line space" for the font size (scriptsize).

A fix I tried (with no success) is to limit the height of the text box (similar to how I limit the width of the box), with the text height keyword, but this does not work.

An ugly solution I can think of is to manually split the text using \\, but also supply a spacing parameter like \\[0.25em], (see Manual/automatic line breaks and text alignment in TikZ nodes) but then I'd have to tweak that for each split...onerous!

What can I do to fix this issue?

bzm3r
  • 3,196

2 Answers2

2

Do not use command for font size together with node text! It should be determined by node parameters:

\documentclass[12pt,letterpaper]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}

\tikzstyle{block} = [draw, rectangle,
minimum height=3em, minimum width=5em, align=left]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]

% The block diagram code is probably more verbose than necessary
\begin{tikzpicture}[auto,>=latex']
% We start by placing the blocks
    \node [block, name=message-source] {message \\ source};
    \node [block, name=encoder, right of=message-source, node distance=3.5cm] {encoder};
    \node [block, name=channel, right of=encoder, node distance=3.5cm, pin={[pinstyle]above:\scriptsize noise},
    node distance=3.5cm] {channel};
    \node [block, name=decoder, right of=channel, node distance=3.5cm] {decoder};
    \node [block, name=user, right of=decoder, node distance=3.5cm] {user};

    % Once the nodes are placed, connecting them is easy.
    \draw [->] (message-source) -- node[name=message, align=center] {\scriptsize message} (encoder);
    \draw [->] (encoder) -- node[name=codeword, align=center] {\scriptsize codeword} (channel);
    \draw [->] (channel) -- node[name=receivedword, align=center, text width=1.75cm,
                                 font=\scriptsize] { received word} (decoder);  <-- here is correct definition of font size!
    \draw [->] (decoder) -- node[name=decodedmsg, align=center, text width=1.75cm] {{\scriptsize decoded message}} (user);
\end{tikzpicture}

\end{document}

I change this only at one node, that you see difference:

enter image description here

Upgrade: An alternative coding (for my exercise, however maybe you will like it) of your MWE:

\documentclass[12pt,letterpaper]{article} \usepackage{tikz} \usetikzlibrary{arrows,chains,quotes}

\usepackage[margin=0.75in,showframe]{geometry}

    \begin{document}
\noindent%
    \begin{tikzpicture}[
    node distance = 7mm and 18mm,
      start chain = going right,
     block/.style = {rectangle, draw, 
                     minimum height=3em, minimum width=5em, align=left,
                     on chain},
every edge/.style = {draw,-latex'},
every edge quotes/.style = {auto, text width=17mm, align=center, font=\scriptsize},
 every pin/.style = {in distance=7mm, font=\scriptsize},
   every pin edge/.style = {black, latex'-},
                        ]
% We start by placing the blocks
\node [block, name=message-source]  {message\\ source};
\node [block, name=encoder]         {encoder};
\node [block, name=channel,
       pin=above:noise]             {channel};
\node [block, name=decoder]         {decoder};
\node [block, name=user]            {user};

% Once the nodes are placed, connecting them is easy.
\draw   (message-source) edge["message"] (encoder)
        (encoder) edge["codeword"] (channel) 
        (channel) edge["received word"] (decoder)
        (decoder) edge["decoded message"] (user);
\end{tikzpicture}
    \end{document}

This, more concise code gives equal result as before. In it I use tikzlibrary quotes for labeling arrows between blocks. All definition of styles are concentrated on begin of picture code (the can be put in \tikzset, \tikzstyle is deprecated). With help of chains library nodes are in "chain" with distances between them determined by node distance.

Zarko
  • 296,517
1

You can use \scriptsize\vbox{received word} to reduce space between lines

\documentclass[12pt,letterpaper]{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}

\tikzstyle{block} = [draw, rectangle, 
minimum height=3em, minimum width=5em, align=left]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]

% The block diagram code is probably more verbose than necessary
\begin{tikzpicture}[auto,>=latex']
    % We start by placing the blocks
    \node [block, name=message-source] {message \\ source};
    \node [block, name=encoder, right of=message-source, node distance=3.5cm] {encoder};
    \node [block, name=channel, right of=encoder, node distance=3.5cm, pin={[pinstyle]above:\scriptsize noise},
    node distance=3.5cm] {channel};
    \node [block, name=decoder, right of=channel, node distance=3.5cm] {decoder};
    \node [block, name=user, right of=decoder, node distance=3.5cm] {user};


    % Once the nodes are placed, connecting them is easy. 
    \draw [->] (message-source) -- node[name=message, align=center] {\scriptsize message} (encoder);
    \draw [->] (encoder) -- node[name=codeword, align=center] {\scriptsize codeword} (channel);
    \draw [->] (channel) -- node[name=receivedword, align=center, text width=1.75cm] {{\scriptsize\vbox{received  word}}} (decoder);
    \draw [->] (decoder) -- node[name=decodedmsg, align=center, text width=1.75cm] {{\scriptsize \vbox{decoded message}}} (user);
\end{tikzpicture}

\end{document} 
Salim Bou
  • 17,021
  • 2
  • 31
  • 76
  • hi salim -- thanks for your answer (I gave it a vote, but I accepted the answer). How did you know that using vbox would work? – bzm3r Jan 14 '16 at 02:26