Use plastic lacing gimp--such as you would use to weave lanyards--instead of yarn to knit a simple case for pencils that won't show marks from the leads and will hold its shape even when it's empty.
Cast on 50 stitches or enough to be sure your piece will be at least nine inches wide.
Knit in stockinet stitch--knit one row, purl one row--until you have a rectangle three inches long.
Continue in stockinet stitch, binding off two stitches at the start of each row until you've come to a point of one or two stitches. Cut off the gimp, leaving a tail at least eight inches long. Thread the tail through the last stitches, remove the needle and knot the gimp tail to itself.
Fold the rectangle portion of the pencil case in half, knit side out. Use lengths of gimp to weave the ends of the new, smaller rectangle together.
Cut another length of gimp twice as long as the tail you left on the point of the flap extending from the closed rectangle. Thread this length through the last stitch and pull the ends even. Braid the new piece and the tail together and knot the ends.
Put up to three pencils in the case, close the flap and wrap the braid around to secure it.
Use the same instructions with string or yarn and line the basic rectangle with plastic. To hold a larger number of pencils, make the basic rectangle in Step 2 deeper by 1/2 inch for each additional pencil. Use a bodkin or tapestry needle to help you work the gimp through the sides to close them.
Because the gimp is less flexible than yarn, larger needles will make it easier to work with, but if you go too big, the pencils can poke through the knitted fabric.