Sometimes, you get a gauge swatch that is not lying, but is instead insisting upon telling you an uncomfortable truth. Despite having carefully planned and knit, washed your swatch, attempted to block it to gauge -- all the right steps -- you find that you just can't get your yarn to match the gauge for your pattern. Or perhaps you can match either stitch gauge or row gauge, but not both. This can often happen with substituting yarn, and sometimes can happen when a designer achieves a gauge toward the limits of a yarn's natural range or otherwise has rather quirky gauge results (Alice Starmore's spectacular colorwork designs are notorious for having hard-to-match gauges, even in the recommended yarns, for instance). When this happens, you'll need to make pattern adjustments so that you end up with fabric of the right size and shape.
Before we get started, here are a few considerations to make sure you're not headed down the wrong path. First, if you are persistently short on row gauge, you may want to make sure you're not twisting stitches. Twisted-stitch fabric draws up and is shorter than untwisted fabric, as well as being less flexible; more than one person has discovered they were accidentally doing combination knitting from exactly this factor. If that's the case with you, you'll be better off addressing this directly, so you only twist stitches when you mean to, rather than adjusting your gauge to compensate for the shorter fabric. Second, if you're substituting yarns, you may want to consider just one more time whether the characteristics of your substitute truly match the characteristics of the original; is it possible that the same factors that are throwing your gauge off will also end in less than desirable results in the finished product? For instance, are you achieving a longer row gauge because your yarn is a heavier fiber with more drape to it, and will that result in a saggy sweater? You may decide that you still think it's a good substitute, or even that the change of characteristics is part of what you're trying to achieve, but it never hurts to stop and make sure you've fully thought things through.
Once you've decided that, yes, compensating for a gauge that's off really is what you want to do, how do you go about that? We'll start with the simpler case, where you can match either stitch or row gauge but not both, and then move to the slightly more complex case where you don't match either.
When you can match only one of stitch or row gauge, you generally want to match your stitch gauge. As a rule, this leads to fewer and simpler adjustments to be made. This may not be the case if you're doing something where row gauge is the greater determinant of fit -- for instance, for a sweater knit side to side -- but in most cases it's going to be true. So for now, we'll assume that you've matched stitch gauge, and you just need to get your row gauge to work.
The easiest case for this is where you have a pattern with no shaping. For instance, look at Jenna Adorno's cute little Tempting sweater. It's form-fitting due to the stretchiness of ribbing, but there's not an increase or decrease in the piece. This pattern also illustrates a common pattern convention, telling you to work until the piece measures a certain length. When you're adjusting for row gauge, what you want to do is figure out how many rows that length translates to -- take your row gauge (if you've measured it over 4", divide it by 4 to give you the gauge over 1", but do not round off yet), and multiply it by the specified length in inches, and then round to the nearest whole number since you can't knit partial rows, and then work that many rows. Pretty easy, and nicely accurate, as long as your row gauge was properly measured. In fact, I'm going to go out on a limb and say that even when you do match row gauge, you're better off making this conversion and knitting to the specified number of rows, especially if you're going to be trying it on as you go and it may be getting stretched out, or if you need to make two pieces that will be seamed together; you're better off doing the math and relying on your carefully controlled measurements from your swatch, rather than hasty measurements on a piece that may be getting temporarily distorted as you work.
It's only a little more work if the pattern gives you a number of rows to work, instead of a measurement. Let's say that instead of saying to knit 14 inches, the pattern told you to work 94 rows, at the pattern gauge of 27 rows/4", and let's say that you're getting 24 rows in 4" instead. You're going to knit (94/27*24) rows -- pattern rows times actual gauge divided by pattern gauge -- which is 83.5; you can't knit half a row, so round up to 83 or down to 84. Half a row, or even a full row, doesn't make a difference in the scheme of things; it's a pretty small fraction of an inch and the eye can't pick that out. This pattern's in the round, so it doesn't matter which way you round off; if you were knitting flat, then whether you wanted to end with a RS row or a WS row might indicate which way to round.
If you have shaping, then you may need to do a little bit more work just on the shaped portions (straight-sided portions can be handled as above) and change the frequency of your shaping rounds so your shaping doesn't end too late or too early. This is not necessarily so -- if the shaping is fairly minimal, or the difference in row gauge isn't very big, then you may decide to ignore the difference. I'd let the decision rest on whether it's big enough to see -- if it's only throwing things off by a single row, maybe even two rows in a fine gauge, then I'd probably ignore it, because you won't see that the slope of the line breaks a row early or continues a row further into the next step, but if it's more than that, it may be visible, and adjustments become worthwhile.
You make these adjustments about the same way you make the adjustments for length in an unshaped area. Take your number of shaping stitches and the number of rows they're occurring across to figure out how many total rows you have in the shaped section, convert that to inches, convert the inches to the rows in your actual gauge, and then redistribute the shaping so it occurs over that number of rows instead. For example, let's say that you're doing a decrease every other row 12 times, and you're using a pattern with a specified gauge of 6 rows/inch. However, you've substituted a cotton that you know from your swatching will shrink lengthwise to 8 rows/inch. Following the pattern, your 12 decreases should be occurring over 24 rows (don't forget to count the plain row after the last decrease), which should occupy 4 inches. Four inches in your actual gauge, however, gives you 32 rows, and if you did the decreases over the first 24 rows, that would leave you with 8 rows, or an entire inch, with no decreases; that's definitely going to be visible, and you definitely need to compensate, which means distributing the decreases over the entire 32 rows. However, 12 doesn't go into 32 evenly; you'd need to do a decrease every 2.7 rows, which is obviously impossible. Fortunately, this is another place where not being able to see that the knitting's a row off one way or the other helps us out, and a line where the decreases are sometimes every 2 row and sometimes every 3 will still look pretty even. Therefore, you could do the decreases with 2 plain rows twice and then a single plain row once, so you're decreasing on rows 1, 4, and 7 of each group of 8, and that would work out okay.
That's a dandy solution if you're working in the round, but if you're working flat, instead of having all your shaping on the RS rows, now you've got some of it on the WS rows. You can do that; this article explains in some detail which decreases match which RS counterparts, but the short form is that you'd use p2tog on the same line where you'd use k2tog, and SSP on the same line where you'd use SSK/SKP. Another possible solution is to let some of the decreases fall still further apart, perhaps using 3 plain rows once and a single plain row twice, so you're decreasing on rows 1, 5 and 7 of each group of 8, but that's more likely to be visible to the eye than the other distribution.
Another possible choice is to break the decreases up into two (or more) groups, with different rates of decrease in each. For instance, you might do your decreases every 3 rows 8 times, and then every 2 row 4 times; another choice would be every 4 rows 4 times, and then every 2 rows 8 times; still another choice would be every 4 rows 3 times, every 3 rows 4 times, and then every 2 rows 4 times. These patterns give you different slopes in the different sections, but because the slope within a section is constant, it'll look intentional and not like an error, which is a possible danger with the more uneven distribution in the paragraph above. It's an aesthetic call, though, and therefore entirely up to you; there's no one "right" way to do this.
That pretty well covers the situation where you're just changing row gauge, so let's turn to those situations where the entire gauge is off. You're going to do this in two steps, first adjusting your stitch gauge, and then doing the same compensation for row gauge outlined above.
For unshaped sections, where the piece is effectively a rectangle, adjusting the stitch gauge is pretty easy, and quite similar to adjusting the row gauge when you're given a specific number of rows. The number of stitches you need is equal to the number of stitches in the pattern, times your actual gauge, divided by the pattern gauge. To go back to the Tempting sweater for an example, let's say you're knitting a size small, which has 160 cast-on stitches, and instead of the pattern's 18 stitches per 4", you've substituted a finer yarn with which you're getting a gauge of 22 stitches per 4". Your revised cast-on number, by the equation, would be (160*22/18), or 195.5. You can't do partial stitches, so you'd need to round up or down, to 195 or 196.
However, there's a potential additional quirk, which is that you may need to have a particular number of stitches for the pattern repeat. In the case of the Tempting sweater, you need a multiple of 4 for the 2x2 ribbing, so 196 it is. You can run into the need to adjust your corrections for the sake of a pattern repeat with row gauge, too, but it's less common; many patterns are only a one row or two row repeat anyway, and even for those that are longer, it's often easy to stop midway through the rows of a repeat.
Once you've adjusted your stitch gauge, you'll also adjust your row gauge, in the same way you'd do it if you were adjusting row gauge alone, and you're ready to knit up this section.
When adjusting stitch gauge on shaped sections, you want to focus on make the same change in the width. There are two ways to go at this: you can calculate the stitches you need for the width at the beginning, then the stitches you need for the width at the end, and subtract the difference; or you can add up the number of shaping stitches, and then figure out what the width of those would have been at the pattern gauge, and then convert that width to the number of stitches in your actual gauge. These should give you the same result, and you might find it useful to calculate it both ways, as a check against arithmetic errors.
Once you've got your number of shaping stitches to do, then you need to do your row gauge conversion, so you know how many rows to distribute your shaping over, and then you want to see if you can evenly distribute them. This step can actually turn out to be pretty easy -- if your stitch gauge and row gauge are both off from the pattern's gauge in the same proportion, then it may well turn out that while you're doing more (or fewer) shaping stitches, you're also doing them over more (or fewer) rows, and the interval at which you need to do them is the same. As an example, let's say you've got a pattern for worsted weight yarn, with stitch gauge of 5st/in and row gauge of 7r/in, and you're converting it to a DK weight yarn at 5.5st/in and 8r/in, and that you've got a shaping section where you do 10 decreases, every other row. Your change in width is (10*5.5/5), which is 11 stitches. The original length over which this occurred was 20 rows; the new length is (20*8/7), or 22.9 -- using the principle that one row either way doesn't make a difference, you can call this 22 rows, and that means that your 11 decreases can also be done every other row, which is a pretty simple change. It won't always be this easy, but often it will, because the proportions of knit stitches tend to be fairly constant even when you change yarn weight, especially when your fiber type is the same.
However, the caveat about stitch patterns may still apply -- for instance, if you're working in seed stitch, you really need to decrease by even numbers to stay in pattern. Applying this to our example above, if it was in seed stitch you'd really like to have either 10 or 12 decreases, and in this case, I'd recommend going with 12, and going up to 24 rows (again, one row either way doesn't matter), so you can still do them every other row, and you stay in pattern. A couple of stitches more or fewer doesn't make a big difference in terms of width, so that's a perfectly fine adjustment to make. However, if you're making several of these adjustments, you want to be sure that together they don't add up to a more significant discrepancy. The easy way to do this is to make sure that if you make one adjustment in the direction of giving you a bigger item, then the next adjustment is made in the direction of giving you a smaller item, and vice versa. For instance, if I rounded up to get an odd number of stitches to do seed stitch in the round, which would tend toward giving me a larger item, then when calculating my decreases I'd want to round toward more decreases, not fewer, which would tend to give me a smaller item, and if I made a third adjustment, I'd lean towards a larger item again, and so on.
Hopefully, this really helps to demystify making these adjustments. Yes, you have to do some planning, and yes, there's some math, but it's just grade-school arithmetic, and you can even use a calculator. You can definitely handle this, and you'll love the results of making your gauge work for you.