WeBWorK Problems

Boolean Circuits using Tikz

by Cindy Loten -
Number of replies: 10

I'm looking to program some questions about Boolean expressions and circuits similar to those in Set theory and logic/Propositional logic/Boolean circuits.  I want to used tikz to create the logic (Boolean) circuits.  I know how to draw graphs (graph theory graphs, not function graphs) using Tikz in Webwork.  When I try to add

circuits.logic.US

to my list of libraries in  tikzLibraries, I get an error.   I have successfully used this library when drawing circuits for my course notes.  Any tips on drawing circuits in webwork?  Examples?  Is there a list of allowable tikz libraries?

Thanks,

Cindy

Re: Boolean Circuits using Tikz

by Glenn Rice -

There are no tikz libraries that are not allowed.  Can you share an example of your code?  Perhaps we can help to figure out what is not working.

Re: Boolean Circuits using Tikz

by Cindy Loten -
Thank-you for your quick response, and I apologize for my slow response. You're right. It's not a library problem. I was having a dollar sign problem. The code below works. However, if try to use
\node[and gate US, draw, logic gate inputs=nn] at ($(P)+(3,0)-(0,0.1)$) (and) {};
to place my add gate like I do in my slides, that results in an error. To do the arithmetic (P)+(3,0)-(0,0.1) to place the AND gate 3 right and 0.1 down of node (P), I need the dollar signs in my slides. Given that $means string name in Perl, what symbol should I put around (P)+(3,0)-(0,0.1) to get tikz to do arithmetic to place the AND gate? Looking at the example provided by Andrew Parker in this thread, I see another approach to placing nodes and gates, but I'm wondering if my current approach can be modified for WeBWorK. DOCUMENT(); loadMacros( "PGstandard.pl", # Standard macros for PG language "MathObjects.pl", "PGML.pl", "PGtikz.pl", #"source.pl", # allows code to be displayed on certain sites. "PGcourse.pl", # Customization file for the course ); # Print problem number and point value (weight) for the problem TEXT(beginproblem()); # Show which answers are correct and which ones are incorrect$showPartialCorrectAnswers = 1;

##############################################################
#
# Setup
#
#

Context("Numeric");

$my_graph = createTikZImage();$my_graph -> tikzLibraries("shapes,decorations,arrows,calc,fit,positioning,arrows.meta,circuits.logic.US");#from slides
#need to look at what libraries I really need
$my_graph -> tikzOptions(" ");$my_graph -> addToPreamble(" ");
$my_graph->BEGIN_TIKZ \node (P) at (0,1) {P}; \node (Q) at (0,0) {Q}; \node[and gate US, draw, logic gate inputs=nn] at (2,.9) (and) {}; %\node[and gate US, draw, logic gate inputs=nn] at ($(P)+(3,0)-(0,0.1)$) (and) {}; \draw (P) -- (and.input 1); \draw (Q) -- (1.5,0) |- (and.input 2); END_TIKZ #using testing suggestion$image = image($my_graph, width => 300, tex_size => 1000); ############################################################## # # Text # # BEGIN_PGML **Having problems with \$ **
Trying to draw a circuit.

[@ $image @]* END_PGML ENDDOCUMENT(); In reply to Cindy Loten Re: Boolean Circuits using Tikz by Sean Fitzpatrick - For math in text nodes, you can do $$\maththings$$ instead of $\maththings$ . In reply to Sean Fitzpatrick Re: Boolean Circuits using Tikz by Sean Fitzpatrick - Sorry for the weird formatting. Moodle also uses the first syntax for entering LaTeX, and I couldn't remember the HTML tag for inline preformatted text. In reply to Sean Fitzpatrick Re: Boolean Circuits using Tikz by Andrew Parker - I was not actually able to get this alternative to work when developing my posted example. It seems that literal dollar signs are required as delimiters for these tikz calculations (please, someone correct me if I'm wrong) as they are not denoting math-mode in this case. However, I was successful in using a variable to hold the literal dollar sign as follows: $dol = '$'; # ... then between BEGIN_TIKZ/END_TIKZ \node[and gate US, draw, logic gate inputs=nn] at ($dol (P)+(3,0)-(0,0.1)$dol ) (and) {}; In reply to Andrew Parker Re: Boolean Circuits using Tikz by Glenn Rice - No, the dollar signs in this case are not for math mode. They are for the "calc" tikz library. You need to escape the dollar signs. You can't use the usual backslash for this due to PG translation. However, you should be able to use ~~ instead. So it would be \node[and gate US, draw, logic gate inputs=nn] at (~~$(P)+(3,0)-(0,0.1)~~$) (and) {}; In reply to Glenn Rice Re: Boolean Circuits using Tikz by Glenn Rice - Here is a working example that uses the double tilde escape approach. DOCUMENT(); loadMacros("PGstandard.pl", "PGML.pl", "PGtikz.pl", "PGcourse.pl");$my_graph = createTikZImage();
$my_graph->tikzLibraries("circuits.logic.US");$my_graph->BEGIN_TIKZ
\node (P) at (0,1) {P};
\node (Q) at (0,0) {Q};
\node[and gate US, draw, logic gate inputs=nn] at (~~$(P)+(3,0)-(0,0.1)~~$) (and) {};
\draw (P) -- (and.input 1);
\draw (Q) -- (1.5,0) |- (and.input 2);
END_TIKZ

# Text
BEGIN_PGML
[@ image($my_graph, width => 300, tex_size => 1000) @]* END_PGML ENDDOCUMENT(); Note that the circuits.logic.US tikz library automatically loads the calc tikz library. So that doesn't need to be explicitly loaded. The other tikz libraries you had listed aren't needed for this (or are also automatically loaded). You can add any back that you use though. In reply to Glenn Rice Re: Boolean Circuits using Tikz by Cindy Loten - ~~ is the fix! Thanks. This is ringing a bell. I just went looking through some notes from last year, and found something about using ~~ (but not \ ) to escape special characters. I was using Perl to process string answers. I'm all set. Good to know I don't have to load calc when I've already got the circuits.logic.US tikz library going. In reply to Cindy Loten Re: Boolean Circuits using Tikz by Andrew Parker - I got the circuits library to work. Here is some sample code: DOCUMENT(); loadMacros( "PGstandard.pl", "PGML.pl", "PGtikz.pl" );$image = createTikZImage();
$image->tikzLibraries('circuits.logic.US');$image->tikzOptions('circuit logic US,line width=0.8pt,line cap=round,line join=round');
$image->BEGIN_TIKZ \matrix[column sep=7mm] { \node (A) {$$A$$}; & & & \\\\ & \node [and gate] (and1) {}; & & \\\\ \node (B) {$$B$$}; & & \node [or gate,yshift=-1mm] (or1) {}; & \node[yshift=-1mm] (out) {};\\\\ \node (C) {$$C$$}; & & & \\\\ }; \draw (A) -- ++(right:5mm) |- (and1.input 1) (B) -- ++(right:5mm) |- (and1.input 2) (or1.input 1) -- ++(left:3mm) |- (and1.output) (or1.input 2) -- ++(left:3mm) |- (C) (or1.output) -- (out); END_TIKZ BEGIN_PGML >> [@ image($image, width => 300, height => 300, tex_size => 450) @]* <<

END_PGML
ENDDOCUMENT();