Quantcast
Channel: Question and Answer » tikz-pgf
Viewing all 959 articles
Browse latest View live

How does the stripcommand in the pgfmanual file work?

$
0
0

I am trying to understand how commands are typeset for use in LaTeX documentation. To clarify, not using LaTeX to create documentation, rather to write documentation in LaTeX about using LaTeX (like in the manuals). As far as I am aware, there is no term of this to distinguish LaTeX documentation from creating documentation with LaTeX (unfortunately).

Lines 1099-1121 of the pgfmanual-en-macros.tex

https://www.ctan.org/pkg/pgf?lang=en

This code “looks like” it removes at signs to me, but I don’t know. Why is this necessary? How does it work?

{
  makeatletter
  globalletmyempty=@empty
  globalletmygobble=@gobble
  catcode`@=12
  gdefgetridofats#1@#2relax{%
    defgetridtest{#2}%
    ifxgetridtestmyempty%
      expandafterdefexpandafterstrippedatexpandafter{strippedat#1}
    else%
      expandafterdefexpandafterstrippedatexpandafter{strippedat#1protectprintanat}
      getridofats#2relax%
    fi%
  }

  gdefremoveats#1{%
    letstrippedatmyempty%
    edefstrippedtext{stripcommand#1}%
    expandaftergetridofatsstrippedtext @relax%
  }

  gdefstripcommand#1{expandaftermygobblestring#1}
}

Reasoning

The reason why I would like to understand this is because I seem to be able to typeset commands with the following code. I suspect there are cases when the my idea will not work.

There are also limitations:

  • include commands with arguments such that I can typeset them (in NewEnviron with #1) as an example under the explanation (e.g. hello{input1} and then using #1 in the NewEnviron would not work)

documentclass{article}
usepackage{fontspec}
usepackage{environ}
NewEnviron{command}[1]{% 
begin{minipage}[t]{.3textwidth}
texttt{string#1}
end{minipage}
hfill
begin{minipage}[t]{.7textwidth}
BODY
end{minipage}
xdefputcommandexample{BODY}% Set BODY to variable http://tex.stackexchange.com/a/14392/13552
}%
begin{document}
section{Friendly Commands}
begin{command}{hello}
This command greets the reader in a friendly manner.
end{command}
begin{command}{goodbye}
This command greets the reader in a friendly manner.
end{command}
end{document}

Sidenote for anybody interested: Instead of minipages I also put the commands in the margin using the marginnote package. It looks alright.

NewEnviron{command}[1]{% 
reversemarginparmarginnote{texttt{string#1}}
BODY
par
}%

Output

enter image description here


avoid hyphenation in chapter title using tikz and titlesec

$
0
0

[This question is similar to the one here, but the solutions given there do not work in my context]

In a (fairly complex) book I’m using titlesec and tikz to produce fancy chapter headings, basically using the following code

titleformat{chapter}[display]
{normalfonthugebfseriessffamily}{scalebox{3}{thechapter}}{25pt}{Hugechaptitle}
%                        left before after
titlespacing*{chapter} {0pt}{110pt}{20pt}
% set the style of the chapter title as a tikzpicture, 
% filled using the current partcol2 color
newcommand{chaptitle}[1]{%
begin{tikzpicture}
  node[fill=gray!20,inner sep=6pt,text width=dimexprlinewidth-12ptrelax] {#1};
end{tikzpicture}
}

However, in several chapters, the chapter title gets hyphenated like this image

enter image description here

I tried to make a simple MWE to demonstrate this, but for some reason it doesn’t hyphenate the word ‘Table’ in this simple example. (The krantz.cls is that used by my publisher to set page dimensions.) FWIW, here it is:

%documentclass[10pt]{book}
documentclass[10pt,krantz2]{krantz}

usepackage{titlesec}
usepackage{blindtext}
usepackage[cmyk]{xcolor}     %% extended color models; load before tikz
usepackage{tikz}             %% used for hyp3way.tex and chapter vtocs

titleformat{chapter}[display]
{normalfonthugebfseriessffamily}{scalebox{3}{thechapter}}{25pt}{Hugechaptitle}
%                        left before after
titlespacing*{chapter} {0pt}{110pt}{20pt}

newsavebox{chaptocbox}

% set the style of the chapter title as a tikzpicture, 
% filled using the current partcol2 color
newcommand{chaptitle}[1]{%
begin{tikzpicture}
  node[fill=gray!20,inner sep=6pt,text width=dimexprlinewidth-12ptrelax] {#1};
end{tikzpicture}
}

begin{document}

chapter[Mosaic Displays for n-way Tables]{Mosaic Displays for n-way Tables}label{ch:mosaic}
blindtext
end{document}

This gives:
enter image description here
One suggested solution was to manually break the title, e.g.,

chapter[Mosaic Displays for n-way Tables]{Mosaic Displays for n-way\ Tables}label{ch:mosaic}

This does have the desired effect, however, it screws up the use of the chapter title in the running heads of the subsequent pages.

So, I’m looking for some way to modify the titleformat or chaptitle commands to say do-not-hyphenate. I did try adding sloppy to the titleformat command.

Command already defined (custom command)

$
0
0

When compiling following code, I the compiler tells me that a command is already defined (the name is c@b...).

documentclass{article}

usepackage{calc}
usepackage{ifthen}
usepackage{tikz}

usepackage{verbatim}

newcommand{piepart}[5]
{
    draw[thick,fill=#4] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

    pgfmathparse{0.5*#1+0.5*#2}
    letmidanglepgfmathresult
    pgfmathtruncatemacro{trunc}{midangle}
    ifthenelse{trunc < 90}{
    draw (midangle:1) node[above right]{#3};
    }
    {
        ifthenelse{trunc < 180}{
        draw (midangle:1) node[above left]{#3};
        }
        {
            ifthenelse{trunc < 270}{
            draw (midangle:1) node[below left]{#3};
            }
            {
                draw (midangle:1) node[below right]{#3};
            }
        }
    }
    pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
    lettemppgfmathresult
    pgfmathparse{max(temp,-0.5) + 0.8}
    letinnerpospgfmathresult
    node at (midangle:0.8) {#5};

}

newcommand{piechart}[1]
{
newcounter{a}
newcounter{b}
foreach p/mcolor/mname in #1
    {
        setcounter{a}{value{b}}
        addtocounter{b}{p}
        piepart{thea/100*360}
                    {theb/100*360}
                    {mname}{mcolor}{p%}
    }
}

begin{document}

begin{tikzpicture}[scale=1.5]
tikzstyle{every node}=[font=tiny]
piechart{{75/gray/A, 18/green/B, 7/red/C}}                 
end{tikzpicture}
begin{tikzpicture}[scale=1.5]
tikzstyle{every node}=[font=tiny]
piechart{{75/gray/A, 18/green/B, 7/red/C}}                 
end{tikzpicture}

end{document}

If somebody knows what creates the error, or how to solve it, could they please tell me.

Thanks

pstool + pgfplots/tikz + parskip conflict resolved

$
0
0

A while back a question about a conflict between pstool, pgfplots and parskip packages was asked. I came across a similar problem using the pstool, tikz and parskip packages in concert, and happened to find a solution to it. However, I couldn’t post the solution on the original thread, since by then the topic was closed (before it was resolved). [edited from here] I therefore re-posted the the issue as a new question, and on Christian’s advice (see comments), my solution is posted below.

Aligned circle around letter – tikz

$
0
0

I am using the following tikz-based command to create a circle around a letter.

newcommand*circled[1]{tikz[baseline=(char.base)]{
        node[shape=circle,draw,minimum size=4mm, inner sep=0pt] (char)
        {#1};}}

It works fine, but whenever I use multiple letters in the same line, they are not aligned.

circle not aligned

I guess it has something to do with the baseline, like the circle is created in reference to the center of mass. Notice that b and d are a bit higher than a and c. How could I fix that?

UPDATE

Following the advice I got from the comments (thank you all), I got the circles aligned to each other (which is what I wanted), but unfortunately not to the text:

alingment

I saw the other post proposing this propnode (the pink a), and I tried to shift it all over the place using the first argument, but it had no effect.

pgfmathsetmacro{nodebasesize}{1} % A node with a value of one will have this diameter
pgfmathsetmacro{nodeinnersep}{0.05}
newcommand{propnode}[5]{% position, name, options, value, label
begin{tikzpicture}
    pgfmathsetmacro{minimalwidth}{sqrt(#4*nodebasesize)}
    node[#3,minimum width=minimalwidth*1cm,inner sep=nodeinnersep*1cm,circle,draw] (#2) at (#1) {#5};
    end{tikzpicture}
}

In fact, propnode is a bit too high and circled is a bit too low. This is the code which calls everything:

(circled{b},circled{a},circled{c},circled{d},propnode{0,4}{n05}{fill=pink,text=black}{0.15}{a}). 

Typesetting two lines of code in a node command

$
0
0

In the following code, TikZ is to draw four nodes at the vertices of a parallelogram. In two of the nodes, I would like to put two lines of text. I indicate this using //. I thought that using the contents option would allow for that.

To draw the edges of the parallelogram, should I first label the coordinates something like A, B, C, and D, and proceed to issue a command like draw (A) -- (B) -- (C) -- (D) -- cycle?

What is the command to put the nodes in the foreground? What is the command to add a bit of space between the border of the node at (2,4) and the two line segments going toward it?

documentclass{amsart}
usepackage{tikz}
        usetikzlibrary{calc,angles,shapes,positioning,intersections,quotes,decorations.markings}        
begin{document}

begin{tikzpicture}

path (0,0) node [red] {origin}
(1,3) node[blue]{node contents={upper \ left}}
(2,4) node[green]{diagonal}
(3,2) node[node contents={lower \ right}] ;

end{tikzpicture}

end{document}

Legend image arguments for the command `addlegendimage`, 3d legend image

$
0
0

In the 3d plot

I want to modify the third legend entry to only show the marks and not the line. From

http://tex.stackexchange.com/a/113765/28093

I understood that I’m supposed to use something like addlegendimage{only marks} for the third entry. However, since the addlegendimage{...} have to be in order of the plots (without forget plot), I still have to know what to use in addlegendimage{...} for the two surfaces. I do not know what arguments I should use to get the nice 3d mesh legend entry. I can’t find it in the pgfplots manual at the documentation of the command addlegendimage{...}.

Furthermore, only wanting to change to only marks and not the color or type of legend entry, with what commands do you retrieve the color and type as used in the addplot command?

Minimum working example:

documentclass{standalone}
usepackage{pgfplots}
begin{document}
    begin{tikzpicture}
        begin{axis}
        [
            xmin=0,xmax=1,xtick={0,1},xlabel=$x$,ymin=0,ymax=1,ytick={0,1},ylabel=$y$,zmin=0,zmax=1,ztick={0,1},legend style={at={(1,1)},anchor=north west,draw=none,fill=none},legend cell align=left,view={45}{10}
        ]

            % What should I put here?
            %addlegendimage{???};
            %addlegendimage{???};
            %addlegendimage{???};

            % phi
            addplot3[mesh,blue,domain=0:1,domain y=1/2:1,samples=5] {0};addlegendentry{plot 1};
            addplot3[mesh,blue,domain=0:1/2,domain y=0:1/2,samples=5,forget plot] {(x-0)*(y-1/2)/((1/2-0)*(0-1/2))};
            addplot3[mesh,blue,domain=1/2:1,domain y=0:1/2,samples=5,forget plot] {(x-1)*(y-1/2)/((1/2-1)*(0-1/2))};

            % phi*2*sin(2*pi*x)        
            addplot3[mesh,red,domain=0:1,domain y=1/2:1,samples=5] {0};addlegendentry{plot 2};
            addplot3[mesh,red,domain=0:1/2,domain y=0:1/2,samples=5,forget plot] {(x-0)*(y-1/2)/((1/2-0)*(0-1/2))*0.6*(sin(deg(2*pi*x))+1)};
            addplot3[mesh,red,domain=1/2:1,domain y=0:1/2,samples=5,forget plot] {(x-1)*(y-1/2)/((1/2-1)*(0-1/2))*0.6*(sin(deg(2*pi*x))+1)};

            addplot3[draw=none,black,mark=o] coordinates { (0,0,0)
                                                            (1/2,0,0)
                                                            (1,0,0)
                                                            (1,1/2,0)
                                                            (1,1,0)
                                                            (1/2,1,0)
                                                            (0,1,0)
                                                            (0,1/2,0)};addlegendentry{plot 3};        
        end{axis}
    end{tikzpicture}
end{document}

How to add a zoombox effect to different images?

$
0
0

According to link it is possible to create magnified subfigures and corresponding boxes for portions of a large image like this one:

enter image description here

I would like obtain the same effect but with different subfigures i.e. not magnified from figure a. It is possible? How?

Thank you in advance!


tikz – drawing an edge that splits to reach two targets

$
0
0

I’m using tikz and want to have an arrow from one node that leaves and goes straight for a while and then splits into two arrows going to different nodes.

Here is a picture I drew a while ago. I now want to replace it with a tikz figure:enter image description here

Notice that one of the vertical edges goes straight for a while and splits into 2. Is there a way to do this cleanly? Let’s say that the source is named source and the targets are named targetA and targetB.

I could specify the in and out angles so that they come out of source with the same slope, but I really want them to be the same edge for a while, not just be close to each other.

Bonus: is there an easy way to make the edge dashed while it overlaps a box to give the impression it’s going behind the box?

Writing the size of a Tikz node to a file

$
0
0

I write a program that typesets elements in a Tikz picture. In general the items are not placed correctly. In order to solve this, I was thinking of running a two-stage compiler: the first time the nodes are rendered using Tikz and the dimensions (width and height) are measured. The results are written to a file and the a constraint logic programming solver (ECLiPSe) aims to find a better orientation for the nodes. Using different approaches, writing the size to the file still doesn’t work:

Minimal Working Example (MWE):

documentclass{article}
usepackage{tikz}
usetikzlibrary{fit,patterns,snakes,calc,decorations,decorations.text,positioning,decorations.pathreplacing,matrix,arrows,automata}
%measure script
makeatletter
newcommandnodedim[1]{
    pgfextractx{nwdt}{pgfpointanchor{#1}{east}}%
    pgfextractx{pgf@xa}{pgfpointanchor{#1}{west}}% pgf@xa is a length defined by PGF for temporary storage. No need to create a new temporary length.
    addtolength{nwdt}{-pgf@xa}%
    pgfextracty{nhgt}{pgfpointanchor{#1}{south}}%
    pgfextracty{pgf@xa}{pgfpointanchor{#1}{north}}% pgf@xa is a length defined by PGF for temporary storage. No need to create a new temporary length.
    addtolength{nhgt}{-pgf@xa}%
}
makeatother

newlengthnwdt
newlengthnhgt

begin{document}

%open the file
newwritespecfile
immediateopenoutspecfile=spec.pl
%draw node
begin{tikzpicture}
node (foo) at (0,0) {This is a test node};
%now measure the node
nodedim{foo}

immediatewritespecfile{rectangle(showthenwdt,showthenhgt).}
end{tikzpicture}
%close the file
immediatecloseoutspecfile
end{document}

The aim is that spec.pl should show for instance:

rectangle(14,4).

with 14 and 4 respectively the width and the height of the node.

Evidently the commands are later wrapped around a macro to generate a full list of rectangle(...). items.

Bounding box is larger than expected when drawing a curved path

$
0
0

I have been working on logo for my department and I have the following code. The problem is that whenever I compile the diagram seems to be larger than the actual logo. I do not know what is the problem. I know the numbers I have selected are not the best but any insights into the matter will be highly appreciated.

 documentclass[letterpaper]{article}
 usepackage[top=2cm,bottom=2cm,left=2cm,right=2cm]{geometry}
 %usepackage{amsmath,amssymb,units}
 %usepackage{enumitem,multicol}
 usepackage{tikz}
 %usetikzlibrary{arrows}
 usepackage{lipsum}

 begin{document}

 lipsum[1-2]
 begin{tikzpicture}[y=0.80pt, x=0.8pt,yscale=-1]
 path[draw=black,fill=white]
   (258.9527,290.5199) .. controls (173.9885,538.4766) and (568.5860,261.2969) ..
   (306.5098,257.1141) .. controls (44.4337,252.9312) and (429.9845,542.5624) ..
   (352.9767,292.0206) .. controls (275.9689,41.4788) and (119.6549,497.6604) ..
   (334.1376,346.9999) .. controls (548.6203,196.3394) and (66.4622,188.6439) ..
   (276.0276,346.0724) .. controls (485.5930,503.5010) and (343.9169,42.5633) ..
   (258.9527,290.5199) -- cycle;
 end{tikzpicture}
 lipsum[1-2]
 end{document} 

See image below:

logo sample

Extrude shapes in tikz 3d

$
0
0

I want to have a shape protruding out of a plane, like the green rectangles here Paint drawing

So far I have a made a box like this:

documentclass[]{article}
usepackage{tikz}
usetikzlibrary{3d}

usepackage[active,tightpage]{preview}
PreviewEnvironment{tikzpicture}
setlengthPreviewBorder{5pt}%

begin{document}

begin{tikzpicture}
     [x={(1cm,-0.9cm)}, y={(5cm,0cm)}, z={(0cm,5cm)}]
 tikzset{zxplane/.style={canvas is zx plane at y=#1,very thin}}
 tikzset{yxplane/.style={canvas is yx plane at z=#1,very thin}}

 begin{scope}[yxplane=-1] %bottom
   draw (-1,-1) rectangle (1,1);
   draw [fill=blue]  (1,0.35) rectangle (0.6,0.8);
 end{scope}

 begin{scope}[zxplane=-1] %left
       draw (-1,-1) rectangle (1,1);
 end{scope}

 begin{scope}[zxplane=1] %right
   draw (-1,-1) rectangle (1,1);
      draw [fill=red] (-0.3, -0.8) rectangle (-0.25,-0.35);
 end{scope}

 begin{scope}[yxplane=1] %top
       draw (-1,-1) rectangle (1,1);
     end{scope}
 end{tikzpicture} 

end{document}

tikzsofar

Is it possible to extrude the red rectangle, so that it comes out of the wall (ideal). Alternative option would be to move the blue one up, so that it is at the same height as the red one, but coming out of the wall. Or maybe there is a special setting for this sort of thing?

Sorry if there are mistakes in my code – it’s a mish mash of stuff I’ve found on here and trial and error on my behalf.

How to reverse battery polarity in tikz circuits library

$
0
0

I can’t find the way to reverse the polarity of a battery symbol in the circuitikz library! Surely there’s a way! Can anybody point in the right direction?

Number format for a node

$
0
0

In the following minimum working example, how do I change number format that is used for the node? For instance, cut off the number after 5 digits.

documentclass{standalone}
usepackage{currfile}
usepackage{filecontents}
usepackage{pgfplots}pgfplotsset{compat=newest}
%usepackage{tikz}

begin{filecontents*}{data.txt}
    640     9     9 1.111111111111111e-01 1.111111111111111e-01 1.234567890000000e+00 3.360958503340160e-03 6.801367190507313e-04 6.801883693667268e-04 1.234567890000000e+00 1.234567890000000e+00
end{filecontents*}

begin{document}
    begin{tikzpicture}
        begin{axis}
            addplot coordinates{(0,0) (1,1)};
            pgfplotstablegetelem{0}{[index]{7}}of{data.txt};edefnodeText{pgfplotsretval};

            % HOW DO I CHANGE THE NUMBER FORMAT FOR THE NODE HERE?
            draw[draw opacity=0] (axis cs:0,0) -- (axis cs:1,1) node[pos=0.5] {nodeText};
            % HOW DO I CHANGE THE NUMBER FORMAT FOR THE NODE HERE?

        end{axis}
    end{tikzpicture}
end{document}

In the pgfplots manual, changing the number format is described for ticks (4.13 Number Formatting Options, Revision 1.12 (2015/01/31)) but not for nodes. How should I do this?

Using math-mode in pgfplots axis unit

$
0
0

I want to apply a unit to my axis. Usually I use the siunitx package for that. However, this time I would like to use a Greek letter with a subscript mu_text{B} but my document won’t compile.

Here is a MWE which compiles if you remove the $s.

documentclass[]{scrartcl}

usepackage[utf8]{inputenc}
usepackage{mathtools}


usepackage{pgfplots,tikz}
pgfplotsset{compat=newest}
usepgfplotslibrary{units}

begin{document}

begin{figure}[tbch]
    centering
    begin{tikzpicture}
        begin{axis}[
            axis line style={very thick},
            axis lines = left,
            y unit ={$A$},
            unit marking pre={text{in }}, 
            unit marking post={},
            ylabel = Yupp
        ]
        end{axis}
    end{tikzpicture}
    end{figure}

end{document}

tikz/pgf upgrade and decorations

$
0
0

Since a major upgrade in pgf, my code does not compile any more.
I am trying to make “decorated” source listing, where the decorations can be
like highlighting some lines (see the MWE), balls on some lines, images, etc.
The decorations are optional, and I pass them as macro arguments. Here goes my MWE

    documentclass{book}
    usepackage{xcolor}
    usepackage{xkeyval}
    usepackage{listings}
    usepackage{tikz}   
usetikzlibrary{shapes,positioning,arrows,fit,backgrounds,calc,tikzmark,shadows}%

    definecolor{ForestGreen}{rgb}{0.0, 0.4, 0.0}
    definecolor{burntorange}{rgb}{0.8, 0.33, 0.0}
    newlength{mywidth} % Used in defining listing width
    definecolor{ivory}{rgb}{1.0, 1.0, 0.94}
        colorlet{SeparatorColor}{burntorange}

        makeatletter
        define@key{MEMacros}{decorations}{defME@decorations{#1}}
        define@key{MEMacros}{language}{defME@language{#1}}
        define@key{MEMacros}{number}{defME@number{#1}}
        presetkeys{MEMacros}{number=1}{}%
    makeatother


    %%Usage MESourceFile[keys]{source file}{caption}{label}
    makeatletter
    newcommandMESourceFile[4][]{
    setkeys{MEMacros}{ %language={[ANSI]C},
     decorations={},#1}% 
          begingroup%
            lstset{    language={[ANSI]C}      }   

             {begin{figure*}[h!btp] }
             caption{#3}
            begingroupedefx{endgroupnoexpandlstinputlisting[label=#4, name=#4] {#2}
                {ME@decorations} % Decorating comments
                }x
            {end{figure*}}
      endgroup%
    }
    makeatother
    % % Prepare for some decorations on the listing files
        makeatletter
        newififlst@linemark

        lst@AddToHook{EveryLine}{%
     begingroup
     advancec@lstnumber by 1relax
     pgfmark{line-lst@name-thec@lstnumber-start}%
     endgroup
        }

        lst@AddToHook{EOL}{pgfmark{line-lst@name-thec@lstnumber-end}%
        globallst@linemarktrue
        }

        lst@AddToHook{OutputBox}{%
     iflst@linemark
     pgfmark{line-lst@name-thec@lstnumber-first}%
     globallst@linemarkfalse
     fi
        }

        deftkzlst@fnum#1relax#2@STOP{%
     def@test{#2}%
     ifx@test@empty
     deftkzlst@start{0}%
     else
     @tempcnta=#1relax
     advance@tempcnta by -1relax
     deftkzlst@start{the@tempcnta}%
     fi
        }

        lst@AddToHook{Init}{%
     expandaftertkzlst@fnumlst@firstnumberrelax@STOP
     pgfmark{line-lst@name-tkzlst@start-start}%
        }

    % % Put a balloon around some lines in a source
    % Usage: MEHighlightLines{BallonName}{SourceName}{FirstLine}{LastLine}
    newcommandMEHighlightLines[4]{%
      pgfmathtruncatemacropgf@temp{%
       #3-1
      }%
      iftikzmark{line-#2-pgf@temp-start}{%
       iftikzmark{line-#2-#3-first}{%
         xdefb@lines{({pic cs:line-#2-pgf@temp-start} -| {pic           cs:line-#2-#3-first})}%
       }{%
         iftikzmark{line-#2-#3-start}{%
           xdefb@lines{({pic cs:line-#2-pgf@temp-start} -| {pic             cs:line-#2-#3-start})}%
         }{%
           xdefb@lines{(pic cs:line-#2-pgf@temp-start)}%
         }%
       }%
      }{%
       xdefb@lines{}%
      }%
      foreach k in {#3,...,#4} {%
       iftikzmark{line-#2-k-first}{%
         xdefb@lines{b@lines (pic cs:line-#2-k-first) }
       }{}
       iftikzmark{line-#2-k-end}{%
         xdefb@lines{b@lines (pic cs:line-#2-k-end) }
       }{}
      }%
      ifxb@linespgfutil@empty
      else
      edefpgf@temp{noexpandtikz[remember picture,overlay]noexpandnode[fit={b@lines}, color=ForestGreen,yshift=-2pt,
         draw, fill=green!30, opacity=0.4,  inner sep=1pt, rounded corners=5pt] (#1) {};
      }%
            pgf@temp
      fi
            }
    makeatother

    begin{document}
    MESourceFile[decorations={
    %MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
    }
    ]{HelloWorld.c}{Hello world}{lst:HelloWorld.c}
    MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
    end{document}

and the great code I want to decorate

#include <stdio.h>

int main()
{
     printf("hello, worldn");
}

The intention of the code is to put a highlighted box around lines 1-3. The code, as is, works. It is just to demonstrate that MEHighlightLines macro works OK. Now you can comment out the pre-last line with that macro and uncomment the line containing the same macro 3 lines above it.

Now you receive the error message

! Undefined control sequence.
pgfmathsetcount ... pgfmath@onquick #2pgfmath@ 
                                                  {afterassignment pgfmath...

Since the only difference is that now the macro is passed as argument, rather then called directly, I guess that the cause is hidden in expanding the macro in the new pgf. The argument passing method is

begingroupedefx{endgroupnoexpandlstinputlisting[label=#4, name=#4] {#2}
    {ME@decorations} % Decorating comments
    }x

The error is somewhere in expanding my passed macros in the upgraded pgf. (The decorations must be kept together with the listing, and so do I force them in a figure. Alternative implementation ideas also welcome (as a workaround), but the best way would be to fix the macro expansion bug.)

TikZ: How to draw an arrow in the middle of the line?

$
0
0

Like this:

.------<------.
|             |
v             ^
|             |
'------>------'

I am currently using

begin{scope}[very thick,->]
  draw (-4,1)--(-4,0)--(0.1,0);
  draw (0,0)--(4,0)--(4,1.1);
  draw (4,1)--(4,2)--(-0.1,2);
  draw (0,2)--(-4,2)--(-4,0.9);
end{scope}

but this is rather inelegant. I prefer something more like

begin{scope}[very thick,middle decoration=>] 
             %           ^^^^^^^^^^^^^^^^^ a hypothetical option
  draw (-4,0)--(4,0);
  draw (4,0)--(4,2);
  draw (4,2)--(-4,2);
  draw (-4,2)--(-4,0);
end{scope}

Draw the plane x+y=1 in three dimensions in pgfplots

$
0
0

I want to draw the plane x+y=1 in pgfplots. In this example, z is free and
is not explicitly a function of x and y. So I cannot use

addplot3[surf, fill=blue, opacity=0.1, fill opacity=0.4] {z=whatever};

I cannot parametrize the plane either, because the following syntax, instructing to take z to be free does not work:

addplot3[surf, fill=blue, opacity=0.1, fill opacity=0.4] {x,-x+1,{}};

How can I do that? Perhaps by using scope and switching x and z axes, so that I can use one of the above standard syntax.

Align vertical tikz chain

$
0
0

I am trying to draw a pinout, and thought a chain would help to create the pin names and pins. However I can not align a tikz chain to the left or right side. I would like the nodes to be aligned based on their left/

documentclass{report}
usepackage{tikz}
usetikzlibrary{scopes}
usetikzlibrary{chains}
begin{document}
begin{tikzpicture}
  { [start chain=1 going below, node distance=1mm, every node/.style={draw}]
    node [on chain] {A};
    node [on chain] {Basd};
    node [on chain] {C};
  } 
end{tikzpicture}
end{document}

TikZ/pgfplots: how can I generate a figure like this one?

$
0
0

What’s the easiest way to produce a figure like this one? The curve is meant to look wiggly and random, and always non-increasing, except for the jumps up. I have tried using decorations with random steps, but that doesn’t look right and is sometimes increasing. I have also tried a plot with a lot of individual coordinates that I generated randomly, but that didn’t look smooth enough.

I’m happy to use either plain TikZ or pgfplots.

enter image description here

Edit: Here are two attempts.

The first uses random steps decoration, but the plot isn’t smooth and it sometimes is increasing. I tried a few different amplitudes.

documentclass{standalone}

usepackage{tikz}
pgfplotsset{compat=1.10}

begin{document}

    begin{tikzpicture}

        draw[->] (0,0) -- (10,0);
        draw[->] (0,0) -- (0,5);

        draw [decorate, decoration={random steps,amplitude=2pt}] (0.2,4) -- (3,1);
        draw (3,1) -- (3,5);
        draw [decorate, decoration={random steps,amplitude=5pt}] (3,5) -- (5,0.2);
        draw (5,0.2) -- (5,3);
        draw [decorate, decoration={random steps,amplitude=8pt}] (5,3) -- (8,1.5);
        draw (8,1.5) -- (8,4);
        draw [decorate, decoration={random steps,amplitude=5pt}] (8,4) -- (9,3.5);

        useasboundingbox (-1,-1) rectangle (11,6);

    end{tikzpicture}

end{document}  

enter image description here

The second attempt uses pgfplots with lots of finely spaced coordinates (which I generated randomly in Excel). This one is closer, but too fine-grained and not smooth enough.

documentclass{standalone}

usepackage{tikz}
usepackage{pgfplots}
pgfplotsset{compat=1.10}

begin{document}

    begin{tikzpicture}

        begin{axis} [
            axis lines=left,
            xtick=empty,
            ytick=empty,
        ]

        addplot [mark size=0]
            coordinates {
                (0.2,4)
                (0.245550438070978,3.9189299356319)
                (0.309894093387146,3.8555584914932)
                (0.374626991695131,3.77679077960278)
                (0.380585874068229,3.74823005668191)
                ... you get the idea ...                
                (11.2737449020538,2.23155401800146)
                (11.2994722948852,2.22522905911657)
                (11.3669785475168,2.17668213475497)
        };

        end{axis}

    end{tikzpicture}

end{document}  

enter image description here

Viewing all 959 articles
Browse latest View live